Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you dispatch on request method in Django URLpatterns?

It's clear how to create a URLPattern which dispatches from a URL regex:

(r'^books/$', books),

where books can further dispatch on request method:

def books(request):
    if request.method == 'POST':
        ...
    else:
        ...

I'd like to know if there is an idiomatic way to include the request method inside the URLPattern, keeping all dispatch/route information in a single location, such as:

(r'^books/$', GET, retrieve-book),
(r'^books/$', POST, update-books),
(r'^books/$', PUT, create-books),
like image 975
Robert Campbell Avatar asked Jun 03 '10 08:06

Robert Campbell


People also ask

What is dispatch method in Django?

The dispatch method takes in the request and ultimately returns the response. Normally, it returns a response by calling (ie, dispatching to) another method like get . Think of it as a middleman between requests and responses.

What is request method == post in Django?

method == "POST" is a boolean value - True if the current request from a user was performed using the HTTP "POST" method, of False otherwise (usually that means HTTP "GET", but there are also other methods).

What is Urlpatterns Django?

Every URLConf module must contain a variable urlpatterns which is a set of URL patterns to be matched against the requested URL.

Which method is used instead of path () in URLs py to pass in regular expressions as routes?

If the paths and converters syntax isn't sufficient for defining your URL patterns, you can also use regular expressions. To do so, use re_path() instead of path() .


2 Answers

The reason it's done as a single view method is that you're usually rendering some kind of page content as context for the form you're about to submit.

Anyway, my reason for replying it this: from your sample URLConf there it looks like you're building a REST webservice with Django -- if this is the case, you might really benefit from using the rather good django-piston to automatically create your resources/collections. It uses class-based handlers that automatically redirect to the appropriate method (get-books, update-books, create-books in your case) based on the HTTP method in the request

UPDATE (four years later!) while django-piston still exists (and works), Django REST Framework is a much more sophisticated, documented and extended choice these days.

like image 145
Steve Jalim Avatar answered Oct 13 '22 23:10

Steve Jalim


Standard Django doesn't have any mechanism for differentiating request methods besides what you used in your second snippet:

if request.method == 'POST':
    ...

However, there are third-party apps and snippets that attempt to make method handling a little cleaner using class based views. See, for example, this snippet (found from this SO question about class views).

Personally I'm not so sure this is a good idea. The standard Django method is so... standard... that I think this introduces extra confusion and complexity where it really isn't needed.

like image 20
Van Gale Avatar answered Oct 13 '22 22:10

Van Gale