Skip to content Skip to sidebar Skip to footer

How To Pass A Variable From Python Django To Html Template

I want to pass a variable from python Django and use it inside the tag. How can I do it? Here is my code. Python- render(request, 'dashboard/dashboard.html', {'variabl

Solution 1:

The problem is django first convert the url, and then you don't have any variable on the code.

Django change this:

{% static"img/icons/vendor/yahoo_weather/{{variable_int}}.gif" %} 

for this:

"/static/img/icons/vendor/yahoo_weather/%7B%7Bvariable_int%7D%7D.gif"

Django first call the "function" to get static urls, and change all the code inside. You can try to call a variable {{ STATIC_URL }} (if you have it defined on settings.py), that variable will get the path for the static folder, and after it will get the value of the variable.

Try this:

<imgsrc="{{ STATIC_URL}}img/icons/vendor/yahoo_weather/{{variable_int}}.gif" %}" /></td>

In this case django should make 2 changes, {{ STATIC_URL}} and {{variable_int}} instead of just one.

Solution 2:

You have not described the problem properly at all. The issue isn't passing it to the template, which is an absolutely basic issue that you obviously already know how to do, but how to use it within a string inside the static tag, which is a completely different question.

The answer is, you can't easily. But why wouldn't you pass the whole path? Why not {"img_path": "img/icons/vendor/yahoo_weather/12.gif"}?

Post a Comment for "How To Pass A Variable From Python Django To Html Template"