[Answered ]-When to use slug field and query string

1👍

Using slugs keep urls simple and clean, thereby easy to remember. Consider the following example:

example.com/post/hello-world/

v/s

example.com/?post=hello-world

Obviously, first one is cleaner.

But query string parameters have their uses too. For example, when you search for an object.

example.com/search/?q=hello-world

or when you need to pass multiple parameters

example.com/search/?q=hello+world&lang=en&something=else
👤xyres

1👍

In slug related django urls you have a url associated to a view. But you cannot pass querystring parameters to your views.

Ex –example.com/post/hello-world/ does not pass any parameter to your view function.

But if you want to pass additional parameters to your views, ex,

example.com/search/?q=hello-world

here q=hello-world is a query string parameter passed to your views.
And inside your views function you can get these parameters in request.GET
So your views function goes something like this

def helloworld():
    qParams = request.GET.get('q', '')
     ....
     ....

Hope this helps.

Leave a comment