How to Render a template in Django
May 12, 2021 2022-12-19 9:49How to Render a template in Django
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.
  ‘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)
    return render(request, “index.html”, context)
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Â