Django working as M – Model, V – View and T – Template
View create two type
Function Based View
A function based view, is a Python function that takes a Web request and returns a Web response.
This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image or anything.
Each view function takes an HttpRequest object as its first parameter.
The view returns an HttpResponse object that contains the generated response. Each view function is responsible for returning an HttpResponse object.
We will call these functions as view function or view function of application or view.
Syntax:-
def function_name (request):
return HttpResponse(‘html/variable/text’)
Function Based Views
Syntax:-
def function_name (request):
return HttpResponse(‘html/variable/text’)
Where HttpResponse is class which is in django.http module so we have to import it before using HttpResponse
views.py
example2
def django2(request):
a = 10 + 10
return HttpResponse(a)
Function Based Views
Single Application with multiple view functions.
views.py
from django.http import HttpResponse
def exampl1(request):
return HttpResponse(‘Hello Django’)
def exampl1(request):
return HttpResponse(‘<h1>Hello Python</h1>’)
urls.py
urlpatterns = [
path(‘urlname1/’, views.exampl1),
path(‘urlname2/’, views.exampl2),
]
********************************
from django.http import HttpResponse
def exampl1(request):
return HttpResponse(‘Hello Django’)
Save views.py
************************
--Posted By : santosh
All Comments...
No comments yet !!!!