Django Templating Templating in Function Based Views

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

You can use a template in a function based view as follows:

from django.shortcuts import render

def view(request):
    return render(request, "template.html")

If you want to use template variables, you can do so as follows:

from django.shortcuts import render

def view(request):
    context = {"var1": True, "var2": "foo"}
    return render(request, "template.html", context=context)

Then, in template.html, you can refer to your variables like so:

<html>
{% if var1 %}
    <h1>{{ var2 }}</h1>
{% endif %}
</html>


Got any Django Question?