Views in Python

A view function, or “view” for short, is simply 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, etc. Example: You use view to create web pages, note that you need to associate a view to a URL to see it as a web page.

In Django, views have to be created in the app views.py file.

Simple View

We will create a simple view in myapp to say "welcome to my app!"

See the following view −

1)

from django.http import HttpResponse

def hello(request):
   text = """<h1>welcome to my app !</h1>"""
   return HttpResponse(text)

In this view, we use HttpResponse to render the HTML (as you have probably noticed we have the HTML hard coded in the view). To see this view as a page we just need to map it to a URL (this will be discussed in an upcoming chapter).

We used HttpResponse to render the HTML in the view before. This is not the best way to render pages. Django supports the MVT pattern so to make the precedent view, Django - MVT like, we will need −

 

2)

A template: myapp/templates/hello.html

And now our view will look like −

from django.shortcuts import render

def hello(request):
   return render(request, "myapp/template/hello.html", {})

Views can also accept parameters −

 

3)

from django.http import HttpResponse

def hello(request, number):
   text = "<h1>welcome to my app number %s!</h1>"% number
   return HttpResponse(text)

When linked to a URL, the page will display the number passed as a parameter. Note that the parameters will be passed via the URL (discussed in the next chapter).

 

 

Cd D:\django\myproject\myapp

Create vi views.py

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse

def hello(request):
text = """<h1>welcome to my app !</h1>"""
return HttpResponse(text)

def index(request):
text = """<h1>welcome to my app !</h1>"""
return HttpResponse(text)

 

###################################################################################

Cd D:\django\myproject\myapp

Create vi views.py

from django.shortcuts import render

# Create your views here.

from django.http import HttpResponse

#Using HttpResponse and without  using .html file

def hello(request):
text="""<h1> Welcome to my app </h1>"""
return HttpResponse(text)

#Using render using .html file

D:\lab\batch31\myproject\myapp\templates\hello.html

def hello(request):
return render(request, "hello.html", {})

# Accepting Arguments in views
def hello2(request,id):
text = "Displaying article Number : %s"%id
return HttpResponse(text)

# Passing arguments to html
D:\lab\batch31\myproject\myapp\templates\hello2.html

def hello3(request,id):
return render(request, "hello2.html", {"today" : id})

 

vi myproject/urls.py and add the following lines

 

from django.contrib import admin
from django.urls import path,include
from . import views

urlpatterns = [
path('admin/', admin.site.urls),
path('display/', views.hello,name='hello'),
path('myapp/', include('myapp.urls')),
]

 

vi myproject/myapp/urls.py and add the following lines

Note: we have to use re_path with regular expressions

from django.contrib import admin
from django.urls import path,re_path
from . import views

urlpatterns = [
#'myapp.views',
#path('admin/', admin.site.urls),
path('', views.hello,name='hello1'),
re_path('display(\d+)', views.hello2,name='id'), 
re_path('temp(\d+)', views.hello3,name='id'),
]

 

cd D:\lab\batch31\myproject\myapp\templates

vi hello2.html

<html>

<body>
Hello World!!!<p>Today is {{today}}</p>
</body>

</html>

 

###################################################################################