The Django view function is responsible to process user request and return a django.http.HttpResponse object to client browser. And view function is defined in Django app views.py file. But besides this, you can also define custom view class to achieve same purpose. Your view class should extends Django provided built-in class-based generic views such as django.views.generic.View
, django.views.generic.ListView
, django.views.generic.DetailView
etc. The customize view class is also written in Django app views.py file. This article will show the difference between class based view and function based view with examples.
1. Django Class Based View.
1.1 Advantage.
- Easy to extends and the code is reusable for customize.
- Use separate method to handle different HTTP request( get, post etc ). Do not need
if else
statement to distinguish user request method in source code. - What you need to do is just extends django buit-in generic view classes, and override the generic view class’s related method or set related attribute value.
- Django Class based view is something like
javax.servlet.http.HttpServlet
class in java, it is a framework class which need you to extends and override methods.
1.2 Disadvantage.
- The code flow is implicit.
- Not easy to read and understand for beginners.
- If you want to use view decorator you need to import extra decorator ( for example :
from django.utils.decorators import method_decorator
) and override methods.
2. Django Function Based View.
2.1 Advantage.
- Code flow is straight forward.
- Easy to use decorators.
- Easy to understand for beginners.
2.2 Disadvantage.
- Can not to extends and hard to make code reusable.
- You need
if else
conditional check to handle different http request method in source code.
3. Djang Class Based View Example.
- Create a Django project and application, you can read article Hello World Django PyCharm Example.
- Create a customize view class extends
django.views.generic.base.View
class in Django application views.py file.
# this is the base generic view class. from django.views.generic.base import View # this decorator is used to disable csrf validation for development case. from django.views.decorators.csrf import csrf_exempt # this class is used to declare method decorator. from django.utils.decorators import method_decorator # decorate the class based view to disable csrf validation. Otherwise when you use curl to send post request to this view will trigger csrf error. @method_decorator(csrf_exempt, name='dispatch') class TestClassBasedView(View): # override base view's get method. def get(self, request): response = HttpResponse('This is a get request\r\n') return response # override base view's post method. def post(self, request): response = HttpResponse('This is a post request\r\n') return response
- Add a new url pattern in urls.py file like below. Pleaese note the second parameter is views.TestClassBasedView.as_view().
url(r'^test_class_based_view$', views.TestClassBasedView.as_view(), name='test_class_based_view'),
- Now test above class based view use curl command in a terminal.
$ curl -i -X POST http://127.0.0.1:8000/dept_emp/test_class_based_view HTTP/1.1 200 OK Date: Wed, 03 Apr 2019 11:06:34 GMT Server: WSGIServer/0.2 CPython/3.6.7 Content-Type: text/html; charset=utf-8 X-Frame-Options: SAMEORIGIN Content-Length: 24 This is a post request ~$ curl -i -X GET http://127.0.0.1:8000/dept_emp/test_class_based_view HTTP/1.1 200 OK Date: Wed, 03 Apr 2019 11:08:32 GMT Server: WSGIServer/0.2 CPython/3.6.7 Content-Type: text/html; charset=utf-8 X-Frame-Options: SAMEORIGIN Content-Length: 23 This is a get request
4. Django Function Based View Example.
- Define below view function in above views.py file.
# below decorator will disable csrf validation for this function based view. Otherwise access this function with post request will trigger csrf error @csrf_exempt def test_function_based_view(request): if(request.method=='GET'): response = HttpResponse('This is a get request\r\n') elif(request.method=='POST'): response = HttpResponse('This is a get request\r\n') return response
- Add below url pattern in urls.py file.
url(r'^test_function_based_view$', views.test_function_based_view, name='test_function_based_view'),
- Test this function view with curl command like class based view.
Reference