Devinwon / article

0 stars 0 forks source link

How to use slug in url #31

Open Devinwon opened 5 years ago

Devinwon commented 5 years ago

Your url will be like this automatically. www.example.com/article/the-46-year-old-man

If you want to use title as slug, django has a simple function called slugify

from django.template.defaultfilters import slugify
class Article(models.Model):
    title = models.CharField(max_length=100)

    def slug(self):
        return slugify(self.title,unique=True)

Also auto slug at django-admin. Added at ModelAdmin:

class ArticleAdmin(admin.ModelAdmin):
    list_display = ('title', 'slug')
    search_fields = ('content', )
    prepopulated_fields = {'slug': ('title', )}