Django – Templates Basic

Lets see how to respond with an html in django. Till now we have seen how to respond to a rest call with a json response. But we will also need to respond to a request with an html/css/javascript. Lets create a small django app to demonstrate the same.

GIT URL – https://github.com/letsblogcontent/Python/tree/master/TemplateDjangoProject

Step 1

Open command terminal and create a directory “TemplateDjangoProject” and navigate into that project

Step 2

First install Django with command “pip install django”

Step 3

Create a project with command “django-admin startporject testproject . “

Step 4

Now create app with command “python manage.py testapp”

Step 5

Add the newly created app in the settings.py file of the testproject folder under the installed_apps array as below to register our new app

Step 6

Now, create a new folder templates under the app testapp folder and create index.html file with content as below

Step 7

Create a function in the views.py file of the app testapp as below

Step 8

update the urls.py(create if not present) file as below. Here we are mapping our url to the function we created in the views.py file in the previous step

Step 9

Now update the urls.py of the our testproject to include the urls created in our app.

Step 10

We are done. Now run the server executing the command “python manage.py runserver” and then navigate to the deploy url to see below response from the server

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: