Search code examples
djangopugpyjade

declare variable in template


Is there any way to declare a variable inside a template? I am pushing the users values for radio buttons, checkboxes and text inputs to the template in the hopes of having their previous selection set as checked/filled in. In this example, their previously saved selection is stored in the db as a number 0-2. This throws an error 'empty variable tag'. Is this possible to do in standard Django templates without pyjade?

div
    - var check = 'checked' if profile_info.selection == 0 else 'unchecked'
    input(type="radio", name="lop", value="0", id="lop1", {{ check }})
div
    - var check = 'checked' if profile_info.selection == 1 else 'unchecked'
    input(type="radio", name="lop", value="1", id="lop2", {{ check }})
div
    - var check = 'checked' if profile_info.selection == 2 else 'unchecked'
    input(type="radio", name="lop", value="2", id="lop3", {{ check }})

Solution

  • You could use the with template tag.

    {% with var=query.id %}
        ... use var in this template scope
    {% endwith %}
    

    You could also create your own template tag.