slug no longer mandatory in form

This commit is contained in:
grisel-davy 2020-12-25 23:33:48 +01:00
parent ac1cdd82e3
commit 2bb50f2930
2 changed files with 16 additions and 2 deletions

View file

@ -11,7 +11,7 @@ class Slug(models.Model):
slug = models.SlugField(
max_length=20,
null=False,
blank=False,
blank=True,
help_text="Slug of the new URL. Up to 20 caracters including letters, number, underscores and hyphens.",)
destination = models.CharField(
max_length=300,
@ -24,3 +24,7 @@ class Slug(models.Model):
date = models.DateTimeField(
auto_now=True,)
def __str__(self):
return '{} -> {:.10}'.format(self.slug,self.destination)

View file

@ -10,6 +10,9 @@ from .settings import TEMPLATES
from .models import Slug
from .forms import SlugAddForm
import random, string
ensemble = string.ascii_letters + string.digits
def index(request):
"""Display the home page of the site.
TODO: Search and display the urls shortened by the user in the past.
@ -24,7 +27,14 @@ def AddSlug(request):
form = SlugAddForm(request.POST)
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('')
else:
form = SlugAddForm()