Views In Django | Python

According to Django documentation 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 . . . or anything, really. The view itself contains whatever arbitrary logic is necessary to return that response. This code can live anywhere you want, as long as it’s on your Python path. There’s no other requirement–no “magic, ” so to speak. For the sake of putting the code somewhere, the convention is to put views in a file called views.py, placed in your project or application directory.

A Django project/ consists of a main folder also know as Django folder which consists of settings.py and one or more app folders which contains views.py.

As shown in image, geeks_site is project folder, brand is an app, db.sqlite3 is default database provided for django and manage.py is python file which runs command to manage changes in project.

Example:

python manage.py runserver
python manage.py startapp
python manage.py migrate
python manage.py makemigrations

Type of Views

There are two type of views in Django.

  1. Function based views
  2. Class based views

In this articles, we will talk about function based views.

Defining and using views –

Every app in Django project by deafult contains a python file named views.py. Using views.py, we control what to display user on webpage.
Now, open brand/views.py and write following lines into it…

filter_none

edit
close

play_arrow

link
brightness_4
code

from django.shortcuts import render
from django.http import HttpResponse
  
# Create your views here.
def ViewDemo(request):
    return HttpResponse("<h1>Hello World. Welcome to views.py.</h1>")

chevron_right


Any function in views.py takes requestparameter by default.
To connect functions defined in views.py, you need to do URL mapping.
Goto geeks_site/urls.py and import ViewDemo fuction from brand/views.py.

from brand.views import ViewDemo

In urlpatterns, add a line

path('hello-world/', ViewDemo)

Now, your geeks_site/urls.py is now,

filter_none

edit
close

play_arrow

link
brightness_4
code

from django.contrib import admin
from django.urls import path
  
from brand.views import ViewDemo
  
urlpatterns = [
    path('admin/', admin.site.urls),
    path('hello-world/', ViewDemo),
]

chevron_right


Now, URL mapping is done. Goto your terminal and start server by using the following commands:

python manage.py runserver

Goto URL http://127.0.0.1:8000/hello-world/ and you will see

Reference: https://docs.djangoproject.com/en/2.2/topics/http/views/



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.