Introduction to Django Views and how to create them(Part VIII)

In this tutorial, we are going to have a look at what Django views and how to create one for your local Blog Web Application.

This tutorial is a part of our series on Creating Blog Website using Django.

Link to the Previous tutorial:

What are Views in Django

Views are basically functions that determine how the information will be displayed on the website. Just like functions, it takes information from Models and then transfers it to Templates to make it visible accordingly on the website.

It takes input in the form of web request and gives out output in form of web response.

In Django, the Views are placed/created in the views.py file.

Creating a View in Django

On your computer open up the file blog1/views.py in your code editor and add to it :

from django.shortcuts import render 
def post(request): 
    return render(request, 'blog/post.html', {})

This will create a View Post that put together the contents of the Template blog/post.html.

Now, go the command line and run the following command :

python manage.py runserver

 

But still, now we are getting an error; something like TemplateDoesNotExist/.

Django Template does not exist

Django: Template does not exist

This is because we haven’t yet created the template required to be rendered.

Don’t worry. That’s something we are going to do in the next tutorial.

Next Part of this Django Tutorial Series:

For now, if you have any doubt drop it in the comment section below.

Also, have a look at some other posts :

Leave a Reply

Your email address will not be published. Required fields are marked *