slug no longer mandatory in form
This commit is contained in:
parent
ac1cdd82e3
commit
2bb50f2930
2 changed files with 16 additions and 2 deletions
|
@ -11,7 +11,7 @@ class Slug(models.Model):
|
||||||
slug = models.SlugField(
|
slug = models.SlugField(
|
||||||
max_length=20,
|
max_length=20,
|
||||||
null=False,
|
null=False,
|
||||||
blank=False,
|
blank=True,
|
||||||
help_text="Slug of the new URL. Up to 20 caracters including letters, number, underscores and hyphens.",)
|
help_text="Slug of the new URL. Up to 20 caracters including letters, number, underscores and hyphens.",)
|
||||||
destination = models.CharField(
|
destination = models.CharField(
|
||||||
max_length=300,
|
max_length=300,
|
||||||
|
@ -24,3 +24,7 @@ class Slug(models.Model):
|
||||||
date = models.DateTimeField(
|
date = models.DateTimeField(
|
||||||
auto_now=True,)
|
auto_now=True,)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return '{} -> {:.10}'.format(self.slug,self.destination)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,9 @@ from .settings import TEMPLATES
|
||||||
from .models import Slug
|
from .models import Slug
|
||||||
from .forms import SlugAddForm
|
from .forms import SlugAddForm
|
||||||
|
|
||||||
|
import random, string
|
||||||
|
ensemble = string.ascii_letters + string.digits
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
"""Display the home page of the site.
|
"""Display the home page of the site.
|
||||||
TODO: Search and display the urls shortened by the user in the past.
|
TODO: Search and display the urls shortened by the user in the past.
|
||||||
|
@ -24,7 +27,14 @@ def AddSlug(request):
|
||||||
form = SlugAddForm(request.POST)
|
form = SlugAddForm(request.POST)
|
||||||
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
form.save()
|
slug = form.save(commit=False)
|
||||||
|
if slug.slug == '':
|
||||||
|
slug.slug = ''.join(random.choice(ensemble) for _ in range(5))
|
||||||
|
|
||||||
|
slug.uid = ''.join(random.choice(ensemble) for _ in range(20))
|
||||||
|
|
||||||
|
slug.save()
|
||||||
|
|
||||||
return HttpResponseRedirect('')
|
return HttpResponseRedirect('')
|
||||||
else:
|
else:
|
||||||
form = SlugAddForm()
|
form = SlugAddForm()
|
||||||
|
|
Loading…
Reference in a new issue