Use of “if” in template with custom template tag with multiple arguments

后端 未结 1 1910
没有蜡笔的小新
没有蜡笔的小新 2021-01-26 08:18

I wrote a custom template tag to query my database and check if the value in the database matches a given string:

@register.simple_tag
def hs_get_section_answer(         


        
相关标签:
1条回答
  • 2021-01-26 09:13

    Set the result of the template tag call to a variable then call {% if %} on that result

    {% hs_get_section_answer questionnaire 'abc' 'def' 'ghi' 'jkl' as result %}
    {% if result %}
    ...
    {% endif %}
    

    You will also need to change your template tag to use an assignment tag instead of a simple tag as well. See assignment tags django doc: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#assignment-tags

    @register.assignment_tag
    def hs_get_section_answer(questionnaire, app, model, field, comp_value):
    
      model = get_model(app, model)
      modal_instance = model.objects.get(questionnaire=questionnaire)
    
      if getattr(modal_instance, field) == comp_value:
        return True
      else:
        return False
    
    0 讨论(0)
提交回复
热议问题