Using string literals as parameters to template tags in Django templates

后端 未结 3 674
南方客
南方客 2020-12-09 19:57

One of the things I find myself doing often is passing string literals as parameters to template tags or functions; for instance:

{% url my-url \'my_param\'          


        
相关标签:
3条回答
  • 2020-12-09 20:41

    Very wierd - I have a django project that uses single quotes to pass a string value and it functions just fine.

    <a href="{% url categories 'vendor' %}"</a>
    <a href="{% url categories 'crew' %}"</a>
    

    On further investigation it turns out this has changed in django 1.5. It now requires the quotes even around the url pattern name.

    0 讨论(0)
  • 2020-12-09 20:42

    This feels wrong but is right.

    <a href="{% url someting "param1" "param2" %}">text</a>
    

    The nested ""'s don't seem like they should work. They do. The Django {% %} material is simply pulled out of the HTML without regard for surrounding context. So the "duplicated" "'s aren't really duplicated at all.

    0 讨论(0)
  • 2020-12-09 20:48

    Use double quotes instead of single quotes:

    {% url my_view "my_param" %}
    
    0 讨论(0)
提交回复
热议问题