django unique slug

Solutions on MaxInterview for django unique slug by the best coders in the world

showing results for - "django unique slug"
Beatrice
16 Sep 2016
1from django.template.defaultfilters import slugify
2
3# in the view maybe while saving the form...
4def savedata()
5    form = someform(request.POST or None)
6  	if form.is_valid():
7        new_post = form.save(commit=False) #for holding it before actually saving
8        # slugify returns elephant-is-big if passed slugify("elephant is big")
9        # and therefore a hyphened slug
10        new_post.slug = slugify(form.title) 
11        new_post.save()
12
13# model may look like this
14class SomeModel(models.Model):
15    title = models.Charfield()
16    slug = models.SlugField(unique=True)
17    ## other fields