'Access a variable from template tag that takes arguments
I have a template tags that takes ID on template and returns a list, I want to check the list if the variable is available and make checkbox input checked.
`_checkbox.html`
{% load get_previous_response %}
{% user_response question.id %} {# this returns a list of ids #}
So I want to do something like this
<input type="checkbox" {% if option.id in user_response %}checked{% endif %}>
problem is the above won't work, I tried django templatetag `with template context`
My main goal is I want to access the list returned by {% user_response question.id %}.
EDIT
My custom templatetag.
get_previous_response.py
from django import template
from survey.models import Question
register = template.Library()
@register.simple_tag(takes_context=True)
def user_response(context, question_id):
question = Question.objects.filter(id=question_id).first()
user = context['request'].user
response = question.get_simple_answer(user)
return response
Solution 1:[1]
You can try:
{% if option.id in user_response %}
<input type="checkbox" checked>
{% else %}
<input type="checkbox">
{% endif %}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sergey Pugach |
