Django Views
- A view is a place where we put our
business logic
of the application - The view is a python function that
takes a web request
andreturns a web response
- This response can be the HTML contents of a
Web page
, or aredirect
, or a404 error
Types of Views
https://medium.com/@ksarthak4ever/django-class-based-views-vs-function-based-view-e74b47b2e41b
- Django views are divided into two major categories:-
- Function-Based Views
- Class-Based Views
FAQ: Describe the use of Django’s class-based views over function-based views.
Function-Based Views
Function-Based Views is very easy to implement
, and it’s very useful but
the main disadvantage is that on a large Django project, usually a lot of similar functions in the views .
If all objects of a Django project usually have CRUD operations so this code is repeated again and again unnecessarily and this was one of the reasons that the class-based views and generic views were created for solving that problem.
# HttpResponse
from django.http import HttpResponse
import datetime
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
# render
from django.shortcuts import render
def home(request):
context = { 'posts' : Posts.objects.all() }
return render(request, 'blog/home.html', context)
Class Based Views(CBVs)
They do not replace function-based views
, but have certain differences and advantages
when compared to function-based views
Pros
Code reuseability
DRY
— Using CBVs help to reduce code duplicationCode extendability
— CBV can be extended to include more functionalities using Mixins
from django.views.generic.list import ListView
from .models import Posts
class GeeksList(ListView):
model = Posts
Django request
- Use
request
instead ofHttpRequest
Attributes
HttpRequest.scheme
# http or https
HttpRequest.body
# All details passed from FE
HttpRequest.path
# '/signup/'
HttpRequest.path_info
# '/signup/'
HttpRequest.method
# Type of request (GET, POST etc)
HttpRequest.content_type
# 'text/plain'
# 'application/x-www-form-urlencoded
HttpRequest.GET
# All GET data in Dict form
HttpRequest.POST
# All POST data in Dict form
HttpRequest.META
# A dictionary containing all available HTTP headers.
HttpRequest.headers
# A case insensitive, dict-like object that provides access to all
# HTTP-prefixed headers (plus Content-Length and Content-Type)
# from the request.
HttpRequest.user
# user data
Methods
HttpRequest.get_host()
# '127.0.0.1:8000'
get_current_site(request).domain
# '127.0.0.1:8000'
HttpRequest.get_port()
HttpRequest.is_secure()
HttpRequest.get_signed_cookie('name')
HttpRequest.build_absolute_uri()
# 'http://127.0.0.1:8000/signup/'
HttpRequest.get_full_path()
# '/signup/'
views.py
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.contrib.auth.forms import (UserCreationForm,
AuthenticationForm, PasswordChangeForm)
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.contrib.auth import login, authenticate
forms.py
from django.contrib.auth.forms import UserCreationForm
from django.forms import ModelForm