Solution 1 :

If I understand correctly your question, you could just call the same template file in your two views functions when rendering:

def your_view_1(request):
    context_1: dict = {'key': 'a_string_depending_on_request_or_view'}
    return render(request, 'common_template.html', context_1)

def your_view_2(request):
    context_2: dict = {'key': 'another_string'}
    return render(request, 'common_template.html', context_2)

Solution 2 :

def view_1(request):
    context_1: dict = {
     //Your key/value pairs
     check = False
    }
    return render(request, 'common_template.html', context_1)

def view_2(request):
    context_2: dict = {
    //Your other key value pairs
    check = True
    }
    return render(request, 'common_template.html', context_2)

And then in your template, you can do an if-else

{% if check %}
//do stuff related to view2
{% else %}
//do stuff related to view1 %}
{%endif%}

Problem :

This is just a question to know about Django. I have two views and I am rendering two contexts to two views.How can I render both the contexts to the same HTML template?

Comments

Comment posted by Sri Test

and how do I add them in urls.py?

Comment posted by Sri Test

and how do I add them in urls.py?Actually I get a problem that I could access only one of the variable in jinja template.One of them doesnt show up any value @bolino

Comment posted by bolino

Just put two “path” or “re_path” in your urls.py, calling each of your views. About the template, it doesn’t need to use all the variables in the context, so you can use only part of them if you want to.

Comment posted by Sri Test

Could you specify an example of above mentioned comment? @bolino

Comment posted by bolino

e.g.

Comment posted by Sri Test

but I already have a dictionary coming from another class which doesn’t have any flags like check

Comment posted by Sri Test

but I already have a dictionary coming from another class which doesn’t have any flags like check

Comment posted by Abhishek bansal

In that case, you can use bolino’s answer if the keys are same, and values are different. However, if keys are also different check whether they are null, and if not then do stuff.

Comment posted by Sri Test

no my keys are different.Why do I have to use a condition.Can’t I directly reference the variable in the HTML file? @Abhishek

Comment posted by Abhishek bansal

@Sri Test Yes you can directly refer them but for someone else reading the code in your template, it won’t make much sense as to what thing is coming from which view. Although I am not understanding what exactly you want to ask here?

By