Create View in Django

In django, a view function is basically used for taking requests from the web and sending responses to the web. The response can be of different types like a html page or just one line httpResponse,etc. To use the view function, we have to use it with a route which will be responsible for calling that particular view.

So let us create a Simple View now.

Open the urls.py in the project folder and add the following code:

from django.contrib import admin
from django.http import HttpResponse
from django.urls import path

def firstview(request):
    return HttpResponse("<h1>My first View in django</h1>")

urlpatterns = [
    path('admin/', admin.site.urls),
    path('funda/', firstview),
]

Here we added a new path in the urlpatterns list with the url as 'funda/'. Now run your project with command "python manage.py runserver" and type the url 'funda/' in the search bar like "http://localhost:8000/funda/" and django will go to the firstview() as given in the path and the HttResponse will be shown on the browser as "My first View in django". 

 

We have successfully got the HttpResponse as the output in the browser. Now we dont want just a single line of response instead we want to return a full .html file as the output. To return the .html as the output, there are a few things that have to be done.

Step 1: Create a file with the name "urls.py" in the app folder and import the files as shown below:

from django.urls import path
from . import views

urlpatterns = [
    path('funda/', views.index),
]

Step 2: Link this file in the main project's urls.py file as shown below:

from django.contrib import admin
from django.http import HttpResponse
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('accounts.urls'))
]

 The path('', include('accounts.urls')) line tells django to search for the urls in our app folder's urls.py file(In our example, the appname is accounts) .So now as have seen in Step 1,we have given the path('funda/', views.index), so this means when we search for the url "funda/", it will go to the views.py file and search for a function called "index()".

Step 3: Open the views.py file in the app folder and create a function with name index() as shown below:

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.

def index(request):
    return render(request, 'accounts/index.html')

Inside the application folder, create a folder with the name "templates".Make sure you write the exact spelling because django will by default search for the templates folder inside the app.

The most important and confusing part: Inside the templates folder create a folder with the same name as of your app.The file structure will look as shown below:

-appname
    -templates
        -appname
            index.html

Example:

-accounts
    -templates
        -accounts
            index.html

This looks redundant but django will look for the appname inside the templates folder.

Create your Index.html file and write some html code in it as shown below:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Funda Djagno</title>
    <style>
        body {
            background-colorlightblue;
        }
    </style>
</head>

<body>
    <h1>This is a HTML page</h1>
    <p>This is a paragraph tag</p>
</body>
</html>

 Now run your project and search for the path given in the urls.py.(http://localhost:8000/funda/) and you will see the html output as shown below:


Tags: Funda of web it Django tutorials, Django tutorials, Django tutorials funda, django for beginners, how to create view in django, what is view in django, templates in django, templating in django

© Copyright - 2018 - 2021 | All rights reserved at Funda of Web IT