Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why SlugField() in Django?

Tags:

django

slug

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 ?

like image 518
Ryu_hayabusa Avatar asked Jul 05 '13 18:07

Ryu_hayabusa


People also ask

What is SlugField in django?

What is SlugField in Django? It is a way of generating a valid URL, generally using data already obtained. For instance, using the title of an article to generate a URL. Let's assume our blog have a post with the title 'The Django book by Geeksforgeeks' with primary key id= 2.

How does django define slug field?

A slug field in Django is used to store and generate valid URLs for your dynamically created web pages. Just like the way you added this question on Stack Overflow and a dynamic page was generated and when you see in the address bar you will see your question title with "-" in place of the spaces.

What is the purpose of __ Str__ method in django?

str function in a django model returns a string that is exactly rendered as the display name of instances for that model. # Create your models here. This will display the objects as something always in the admin interface.

What is the use of slug?

A slug is a human-readable, unique identifier, used to identify a resource instead of a less human-readable identifier like an id . You use a slug when you want to refer to an item while preserving the ability to see, at a glance, what the item is.


1 Answers

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() 
like image 127
allcaps Avatar answered Oct 23 '22 01:10

allcaps