Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django distinguish between get / post in view's methods

I've been working with other languages where I could simply have one method name (in controller) for one action (lets say login). For example in spring MVC I can have one method named 'login' and set method type (get or post) via annotation property. Is this possible in Django? For example, I have this method login:

def login(request):
    return render(request, 'login.html')

This method is accesed via GET, do I need to declare new method i.e login_post(request) for post accessing? or should i check if request.POST['value'] is not empty in the first method, if it is not then it is POST, if it is empty then it should be GET. I am new to Django, how are your suggestions? thanks.

like image 246
Pink Avatar asked Nov 06 '15 19:11

Pink


2 Answers

There's no need to create functions for each one, you can "ask" the request:

You can:

def login(request):
    if request.method == 'POST':
        # Your code for POST
    else:
        # Your code for GET
    return render(request, 'login.html')

Or, you can assume GET as the default:

def login(request):
    if request.method == 'POST':
        # Your code for POST
        # make sure to put a "return redirect" statement here
    # Your code for GET
    return render(request, 'login.html')

Both are okay. Also, take a look at Class-based Views as an alternative, they are very helpful.

like image 104
César Avatar answered Nov 13 '22 16:11

César


As mentioned in the Django documentation, another approach would be to used class-based views.

Class-based views provide an alternative way to implement views as Python objects instead of functions. They do not replace function-based views, but have certain differences and advantages when compared to function-based views:

  • Organization of code related to specific HTTP methods (GET, POST, etc) can be addressed by separate methods instead of conditional branching.
  • Object oriented techniques such as mixins (multiple inheritance) can be used to factor code into reusable components.

So instead of using function-based view (as mentioned in other answers):

from django.shortcuts import render

def login(request):
    if request.method == 'POST':
        # handle the post request
     else:
         # handle the get request
     return render(request, 'login.html')

You could use a class-based view like so:

from django.shortcuts import render
from django.views.generic import View

class LoginView(View):
    def post(self, request):
        # handle the post request
        return render(request, 'login.html')

    def get(self, request):
        # handle the get request
        return render(request, 'template-path.html')

When using class-based views, your urls.py would look like this:

# urls.py
from django.conf.urls import url
from myapp.views import LoginView

urlpatterns = [
    url(r'^login/', LoginView.as_view()),
]
like image 35
strix Avatar answered Nov 13 '22 16:11

strix