Home > Net >  Django isn't evaluating template variables
Django isn't evaluating template variables

Time:01-21

When I inspect the page I see: post="{% url daySlots %}" But Im expecting to see this evaluated to: post="/daySlots/" Its also ignoring other variables, the html shows the variable name like: {{ duration }} instead of the value like 1.5 that Im expecting.

in my views.py the html code is generated in a function like:

html_cal = Calendar(today.year, today.month, day_colors)

context = {
        "html_cal": mark_safe(html_cal)
}

response = render(request, "pages/book.html", context)

book.html looks like

{% block content %}
  <div >
    {{ html_cal }}
  </div>
{% endblock content %}

The Calendar function also has some formatted text like: td = f"post=\"{{% url daySlots %}}\"

Any ideas on how to get Django to recognize the variables within the html?

CodePudding user response:

You can pass a compiled Template to the {% include %} tag, this way you can generate template fragments like yours and render them in another template with the current context.

from django.template import Template


html_cal = Calendar(today.year, today.month, day_colors)
context = {
    "html_cal": Template(html_cal)
}
response = render(request, "pages/book.html", context)

In your template

{% block content %}
  <div >
    {% include html_cal %}
  </div>
{% endblock content %}
  •  Tags:  
  • Related