Search code examples
djangourlslug

slugfield vs. regex - should I add a slug to models?


My models don't currently have a slugfield in them. So far, when I use a table value in the url such as /movie/Gone With The Wind, I'd just use a regular expression to get the value I want in the url.

(r'^movie/(?P<movie_nm>[\w|\W]+)/$', movie_page),

So far, this has worked out fine for me.

Is it advisable to go back and add a slugfield? I don't want to go through the hassle as I don't see the downside to the current method.


Solution

  • That depends on what your data looks like and how pretty you want your URLs to be. If you have a movie name that has a character other than the \w set (including most punctuation), your URL pattern will break.

    Also, remember that your titles will need to be URL escaped--if you care about the aesthetics, /movie/Gone-With-The-Wind looks a lot better than /movie/Gone%20With%20The%20Wind.