Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

slug - Why SlugField() in Django?

Django has models.SlugField() which helps us to create some cool looking urls. My question is why specifying it as a field

suppose I have this model

 class Blog(models.Model):
    title = models.CharField()

and if I want to add slug, I could just use

 class Blog(models.Model):
    title = models.CharField()

    def title_slug(self):
       return slugify(self.title)

in urls I could just use

(r'^blog/(?P<id>d+)/(?P<slug>[-w]+)/$', 'app.views.blog_view'),

and in views

def blog_view(request, id ,slug):
    get_object_or_404(Blog, pk=id)
    ...

urls will look like

example.com/blog/23/why-iam-here/

There are three things that makes me adopt this method

  1. Slug field does not comes with implicit uniqueness.
  2. get_object_or_404(Blog, pk=id) must be faster than get_object_or_404(Blog, slug=slug).
  3. Adding a slug field to existing models involves data migration.

so why SlugField()? , apart from the cost of dynamically generating slug, what are the disadvantages of above method ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why SlugField() in Django? Because:

  1. it's human friendly (eg. /blog/ instead of /1/).
  2. it's good SEO to create consistency in title, heading and URL.

The big disadvantage of your dynamically generated slug, accepting slugs in urls.py and not using the slug to get the right object? It's bad design.

If you supply and accept slugs, but don't check against them, then you have multiple URLs returning the same content. So /1/useful-slug/ and /1/this-is-a-bs-slug/ will both return the same page.

This is bad because it does not make life easy for humans. Your visitors have to supply an id and something that is redundant. Duplicated pages are search engines nightmare. Which page is the right one? Duplicated pages will end up with a low rank. See https://support.google.com/webmasters/answer/40349?hl=en (last p)

You can argue that you implement your own beautifully generated links consistently, but people and bots guess URLs all the time (see your logfiles). When you accept all slugs then humans and bots always guess right.

Also saving a slug to the db saves processing power. You generate the slug one time and reuse it. What will be more (in)efficient looking up the slug or generating it each time?

Slug fields in the admin are useful to give editors the opportunity to edit the slug. Maybe to supply extra information that is not in the title but still worth mentioning.

Bonus: To update migrated data:

from django.template.defaultfilters import slugify

for obj in Blog.objects.filter(slug=""):
    obj.slug = slugify(obj.title)
    obj.save()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...