Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django GET and POST handling methods

Tags:

python

django

I want a way to automatically route GET and POST requests to subsequent methods in a centralized way. I want to create my handler in the following way.

class MyHandler(BaseHandler):
    def get(self):
        #handle get requests

    def post(self):
        #handle post requests

This is what webapp2 does and I very much like the style, is it possible to do in Django? I also want the view in Class-method style. What kind of BaseHandler and router should I write.

HINT: Use django generic views.

like image 452
specialscope Avatar asked May 21 '13 11:05

specialscope


People also ask

How can I get POST request in Django?

Django pass POST data to view The input fields defined inside the form pass the data to the redirected URL. You can define this URL in the urls.py file. In this URLs file, you can define the function created inside the view and access the POST data as explained in the above method. You just have to use the HTTPRequest.

How do you receive data from a Django form with a POST request?

Using Form in a View In Django, the request object passed as parameter to your view has an attribute called "method" where the type of the request is set, and all data passed via POST can be accessed via the request. POST dictionary. The view will display the result of the login form posted through the loggedin.

What does get method do in Django?

GET is an HTTP method in Django that encapsulates the data in a string and utilizes it to construct a URL. The URL includes the data keys and values as well as the address to which the data must be transmitted.

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).


1 Answers

This is supported in Django as class based views. You can extend the generic class View and add methods like get(), post(), put() etc. E.g. -

from django.http import HttpResponse
from django.views.generic import View

class MyView(View):
    def get(self, request, *args, **kwargs):
        return HttpResponse('This is GET request')

    def post(self, request, *args, **kwargs):
        return HttpResponse('This is POST request')

The dispatch() method from View class handles this-

dispatch(request, *args, **kwargs)

The view part of the view – the method that accepts a request argument plus arguments, and returns a HTTP response.

The default implementation will inspect the HTTP method and attempt to delegate to a method that matches the HTTP method; a GET will be delegated to get(), a POST to post(), and so on.

By default, a HEAD request will be delegated to get(). If you need to handle HEAD requests in a different way than GET, you can override the head() method. See Supporting other HTTP methods for an example.

The default implementation also sets request, args and kwargs as instance variables, so any method on the view can know the full details of the request that was made to invoke the view.

Then you can use it in urls.py -

from django.conf.urls import patterns, url

from myapp.views import MyView

urlpatterns = patterns('',
    url(r'^mine/$', MyView.as_view(), name='my-view'),
)
like image 154
Bibhas Debnath Avatar answered Oct 18 '22 12:10

Bibhas Debnath