Search code examples
pythondjangotemplatescustom-tags

Get a variable out of a 'Model' -- Django Template


I have a complicated question, so I hope I can write it in a understandable way. I have a template (index.html) and I'm using a custom tag for setting a few variable.

I can set this varible with:

{% set table_breaker_houses = 4 %}

And I use it later many times with, for example:

{% if forloop.counter <= table_breaker_houses %}

This is how it works now, but I want something like this:

{% for settings in Settings %}
{% set table_breaker_houses = "{{ settings.table_breaker_houses }}" %}
{% endfor %}

{% if forloop.counter <= table_breaker_houses %}

"Settings" is a model with values, so I can manage my settings with the adminpanel.

Could someone please help me out? Thanks for readings so far and sorry for my bad english. :)

With best regards, borsTiHD


Solution

  • You don't need the quotes and {{}}.

    Use the variables directly within the tag:

    {% for settings in Settings %}
       {% set table_breaker_houses = settings.table_breaker_houses %}
    {% endfor %}
    

    Also, be sure Settings is a queryset or an iterable, and not just a model.