This section covers how to set up views in your django website. Please refer to template section of this website as well as this section to set up your views properly.
Please note that this is site provides the most commonly used django features. It is by no means intended to be an exhaustive documentation of the Django framework. You can always use the official django documentation by clicking the big green button below.
Django Views DocsIt's a best practice to render templates along with your views. Setting up a template is easy.
In <app_name> directoy, add a directoy called templates:
cd <app_name> && mkdir templates
Then in your <app_name>/templates directoy (that you just created),
add another directoy called templates:
cd templates && mkdir <app_name>
The next step is to add an html file. In the currend directoy, type:
touch <template_name.html>
You may now add any content to this html file. Feel free to use the
template section of this website as a guide
In a django website, views return data (and templates), which become the content of your routes. The first step to creating a view is to make sure you have imported the render function from django.shortcuts. To do this, go into the views.py file in your app, and near the top of the page, add:
from django.shortcuts import render
Then define a function that accepts request as a parameter. You can call this function whatever you like, but it would be customery to call it index.
def index(request):
Then your function will return the render function with three arguments: the request, your template, and any data that you want it to return in a dictionary:
return render(request,
<app_name.html>/<template_name.html>,
{"<your_key>": <your_value>})
With all of the above in place, code inside your views.py will look something like below. Obviously, the naming will depend on the names that you chose for your funtions, data, and file names
from django.shortcuts import render
def index(request):
return render(request,
<app_name.html>/<template_name.html>,
{"<your_key>": <your_value>})