Django template variable not rendering

1.6k views Asked by At

So I am trying to render a django template variable, but for some reason it is not rendering out.

I have the following code:

Views.py

...
def get_search(request, *args, **kwargs):
    context = {}
    context = {"test":"abcde"}
    return render(request, 'search.html', context)
...

Urls.py

urlpatterns = [
    path("", home_view),
    path("articles/", include("papers.urls")),
    path("admin/", admin.site.urls),
    path("search/", search_view),
    path("submit/", submit_article_view, name="submit"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

search.html

{extends 'base.html'}
{% block content %}
<p> {{ test }} hi </p>
...
{% endblock %}

base.html

{% load static %}
<!DOCTYPE html>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" type ="text/css" href="{% static 'css/stylesheet.css' %}">
</head>
<header>
{% include 'navbar.html' %}
</header>
<body>
    {% block content %}

    {% endblock %}
</body>
<footer>
{% include 'footer.html' %}
</footer>
</html>

Now, whenever I run the server and go to the search page, everything else gets rendered out, but the {{ test }} doesn't. The 'hi' does get rendered out. I have followed every tutorial and StackOverflow question out there, to no avail. Anyone has any idea what might be going on?

Also, StackOverflow newbie, let me know if I did something wrong regarding that :).

1

There are 1 answers

0
Daniel Mol On BEST ANSWER

The problem was that I was rendering a different view, which also used the search.html. Hence why everything was the same, except for the template variable. With now putting the context into the correct view, search_view it renders properly.

search_view

def search_view(request, *args, **kwargs):
    context = {}
    context = {"test":"abcde"}
    return render(request, 'search.html', context)