How to Render a template in Django

How to Render a template in Django copy
Django Django Tutorials

How to Render a template in Django

How to Render a template in Django

In the Django tutorial for beginners series, we have to learn how to render a template in Django project in order to show the website’s content to users. Because without a template your website/ application cannot be developed. Template is the HTML, CSS, Boostrap and JS combinaiton web page that dispaly to user as frontend. There are different techniques we can follow to design a template, but we will render a template in the Django project.

When a user hits a URL, it goes to the server as a request for getting a web page, then the server gives a response to the user browser, then displays that web page.

 

How to Render a template in Django? After installation of your Python Django web framework library, you have to make a mindset, which project you want to create then, you have to start a Django project.

Next! Create a template folder in your Django application. Your template folder will not work till you include your template in the Template list in the settings.py file in the Django project, because you have to tell the engine that your template folder is in the base directory. So follow the below code of line below to include your template folder (folder and template files).

  ‘DIRS’: [os.path.join(BASE_DIR,‘templates’)]

Before adding the above line, in your project’s settings.py file. The template list will be like this:

TEMPLATES = [  

    {  

‘BACKEND’‘django.template.backends.django.DjangoTemplates’,  

‘DIRS’: [os.path.join(BASE_DIR,‘templates’)],  

‘APP_DIRS’: True,  

‘OPTIONS’: {  

‘context_processors’: [  

‘django.template.context_processors.debug’,  

‘django.template.context_processors.request’,  

‘django.contrib.auth.context_processors.auth’,  

‘django.contrib.messages.context_processors.messages’,  

            ],  

        },  

    },  

]  

Just copy the following code and paste it into your settings.py file. And replace the code with the following code. You can also paste only one line code as we mentioned above.

TEMPLATES = [  

    {  

‘BACKEND’‘django.template.backends.django.DjangoTemplates’,  

‘DIRS’: [os.path.join(BASE_DIR,‘templates’)],  

‘APP_DIRS’: True,  

‘OPTIONS’: {  

‘context_processors’: [  

‘django.template.context_processors.debug’,  

‘django.template.context_processors.request’,  

‘django.contrib.auth.context_processors.auth’,  

‘django.contrib.messages.context_processors.messages’,  

            ],  

        },  

    },  

]  

 

How to render a template in Django views section

To render a template you have to write a views.py file in the view section don’t matter you are using function base or class base view. When a user hits the URL, then the server gives a response on the user’s requeset in the form of a frontend template. For this, you have to write a line of code at the end of the view function e.g

def jafricode_view(request):
    return render(request, “index.html”) # This line will render a template, as the file exists in its locaiton

if you notice that, in the last line of code, you are seeing there is a request argument, it means you are requesting a server to give a response for a template named “index.html”. So you will get a template index.hmtl in your browser.

In the Django project, we can render the template with context, context means you can pass data in the form of a dictionary from the views section, and passing in a context you can use that dictionary data in the template (HTML CSS designing)

def jafricode_view(request):
     context = {‘first_name’: ‘John’}
    return render(request, “index.html”, context)
So, we can use the first_name key in the index.html as a variable, we will see a result on the browser as ‘John’. Just put the variable like this {{first_name }}
You have noticed we used {{}} like syntax. It is called the Django variable in the Django template language. You can learn it in detail from our blog.

Conclusion

In short, we have learned how to render a template in the Django project, which is very easy to do. Just focus on the last line of code as you analyzed in the view section.

If you have any confusion yet, then without hesitation, discuss with us.

Please share this lesson with your friends on social media networks.

Recommended Django tutorial for beginners

Django Tutorial for Beginners | Django Tutorials series 

Difference between MVC and MVT architecture design patterns

Ultimate Guide on Django History | Django Tutorials Series

Leave your thought here

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

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the comparison bar
Compare

Get your Enrollment

50%OFF

Leave your details below and receive a discount coupon in your inbox