Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL path parameters vs query parameters in Django

Tags:

python

django

I've looked around for a little while now and can't seem to find anything that even touches on the differences. As the title states, I'm trying to find out what difference getting your data via url path parameters like /content/7 then using regex in your urls.py, and getting them from query params like /content?num=7 using request.GET.get() actually makes.

What are the pros and cons of each, and are there any scenarios where one would clearly be a better choice than the other?

Also, from what I can tell, the (Django's) preferred method seems to be using url path params with regex. Is there any reason for this, other than potentially cleaner URLs? Any additional information pertinent to the topic is welcome.

like image 362
gucciferXCIV Avatar asked Oct 20 '17 16:10

gucciferXCIV


People also ask

What is the difference between URL parameter and query parameter?

URI parameter (Path Param) is basically used to identify a specific resource or resources whereas Query Parameter is used to sort/filter those resources. Let's consider an example where you want identify the employee on the basis of employeeID, and in that case, you will be using the URI param.

What is the difference between URL parameters and query strings?

Parameters are key-value pairs that can appear inside URL path, and start with a semicolon character ( ; ). Query string appears after the path (if any) and starts with a question mark character ( ? ). Both parameters and query string contain key-value pairs.

What is the difference between path and URL in Django?

In Django 2.0, you use the path() method with path converters to capture URL parameters. path() always matches the complete path, so path('account/login/') is equivalent to url('^account/login/$') . The part in angle brackets ( <int:post_id> ) captures a URL parameter that is passed to a view.

Does URL path include query?

pathname. The pathname property of the URL interface is a string containing an initial / followed by the path of the URL, not including the query string or fragment (or the empty string if there is no path).


1 Answers

This would depend on what architectural pattern you would like to adhere to. For example, according to the REST architectural pattern (which we can argue is the most common), you want do design URLs such that without query params, they point to "resources" which roughly correspond to nouns in your application and then HTTP verbs correspond to actions you can perform on that resource.

If, for instance, your application has users, you would want to design URLs like this:

GET /users/ # gets all users
POST /users/ # creates a new user
GET /users/<id>/ # gets a user with that id. Notice this url still points to a user resource
PUT /users/<id> # updates an existing user's information
DELETE /users/<id> # deletes a user

You could then use query params to filter a set of users at a resource. For example, to get users that are active, your URL would look something like

/users?active=true

So to summarize, query params vs. path params depends on your architectural preference.

A more detailed explanation of REST: http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api

Roy Fielding's version if you want to get really academic: http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

like image 100
slider Avatar answered Sep 28 '22 13:09

slider