Rendering HTML template as response — Django Views

A view function, or view for short, is simply a Python function that takes a web request and returns a web response. This article is about how to render an HTML page from Django using views. Django has always been known for its application structure and its ability to easily manage applications. Let's take a look at how to render a template file in a Django view.

Django renders an HTML template as an explanation of the response

An illustration of how to render an HTML template in a view using example. Consider a project named pythonengineering that has an application named geeks .

Refer to the following articles to check how to create a project and an app in Django.

Once you have your project and application, open and let's start creating a view called geeks_view that is used to print “ Hello world ”through the template file. The django view — it is a python function that takes an argument called request and returns a response
Enter the following code into the of the application

but this code will not work until the correct URL mapping is determined. Mapping means that you need to tell Django what the user is entering into the browser to render your particular view. For example, tells django to view the home page. So let's change to start our browsing. 
Include your app URLs in base URLs by adding the following code to pythonengineering & gt;

from django.shortcuts import render

# Create your views here.

def geeks_view (request) :


# the render function takes a request argument

  # and return HTML as response

return render (request, "home.html" )

& quot; & quot; & quot; pythonengineering URL config

The 'urlpatterns' list directs URLs to representations. For more information, please see: / en / 2.2 / themes / http / urls / Examples:

Functional Views

  1. Add imports: from my_app import views

2. Add url to urlpatterns: path (& # 39; & # 39 ;, views.home, name = & # 39; home & # 39;)

Class-Based Views

1. Add imports: from other_app.views import Home

2. Add url to urlpatterns: path (& # 39; & # 39 ;, Home.as_view (), name = & # 39; home & # 39;)

Including other URLconf

  1. Import the include () function: from django.urls, import include, path

2. Add URL to urlpatterns: path (& # 39; blog / & # 39 ;, include (& # 39; blog.urls & # 39;))

"" "

from django.contrib import admin

from django.urls import path, include


urlpatterns = [

path ( 'admin /' ,,

path ('' , include ( "geeks.urls" )),


Tep Now let's create a path to our view from geeks & gt;

from django.contrib import admin

from django .urls import path

# importing views from

from . views import geeks_view


urlpatterns = [

path ('', geeks_view),


Completed. Now go to check if our template is showing or not. visit here —  http:// localhost: 8000 /

It gives an error that you do not have a home.html template. So let's create our template now, in the geeks folder create a folder called templates and create a home.html file in it. Now let's check again —  http:// localhost: 8000 / .

This way you can render any template file using the same procedure —

 1. Create a view in 2. Create a template file which is to be rendered and link it to the view. 3. Create a URL to map to that view. 

We used this error because while learning how to create a project using django? You may encounter a lot of errors, you don't need hyper there, just take a deep breath and just google error, you will have a solution for everything.

Get Solution for free from DataCamp guru