0

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 :).

  • Might not solve the problem but you are missing `%` in `{extends 'base.html'}` i.e. it should be `{% extends 'base.html' %}` – Abdul Aziz Barkat May 29 '21 at 09:14
  • Your view is called `get_search`, while your URL pattern references `search_view` - this may be a problem. Does `search_view` maybe not pass the `test` variable in its context? – sk1p May 29 '21 at 09:19
  • It was in correct in the main code Abdul, sorry for mistyping and ty for helping! Though the problem was indeed what ski1p mentioned, I was using a different view which also rendered search.html so now I feel a bit stupid... Thank you both for the help! – Daniel Mol May 29 '21 at 09:28

1 Answers1

0

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)