How to put comments in Django templates

后端 未结 6 1203
清歌不尽
清歌不尽 2020-12-04 07:46

I would like to comment this with a line

{% if something.property %}
    ...



{% # this is a comment %}
{% if something.pr         
相关标签:
6条回答
  • 2020-12-04 08:31

    Multiline comment in django templates use as follows ex: for .html etc.

    {% comment %} All inside this tags are treated as comment {% endcomment %}
    
    0 讨论(0)
  • 2020-12-04 08:33

    Comment tags are documented at https://docs.djangoproject.com/en/stable/ref/templates/builtins/#std:templatetag-comment

    {% comment %} this is a comment {% endcomment %}
    

    Single line comments are documented at https://docs.djangoproject.com/en/stable/topics/templates/#comments

    {# this won't be rendered #}
    
    0 讨论(0)
  • 2020-12-04 08:35

    Using the {# #} notation, like so:

    {# Everything you see here is a comment. It won't show up in the HTML output. #}
    
    0 讨论(0)
  • 2020-12-04 08:46

    As answer by Miles, {% comment %}...{% endcomment %} is used for multi-line comments, but you can also comment out text on the same line like this:

    {# some text #}
    
    0 讨论(0)
  • 2020-12-04 08:49

    This way can be helpful if you want to comment some Django Template format Code.

    {#% include 'file.html' %#} (Right Way)

    Following code still executes if commented with HTML Comment.

    <!-- {% include 'file.html' %} --> (Wrong Way)

    0 讨论(0)
  • 2020-12-04 08:53

    In contrast to traditional html comments like this:

    <!-- not so secret secrets -->
    

    Django template comments are not rendered in the final html. So you can feel free to stuff implementation details in there like so:

    Multi-line:

    {% comment %}
        The other half of the flexbox is defined 
        in a different file `sidebar.html`
        as <div id="sidebar-main">.
    {% endcomment %}
    

    Single line:

    {# jquery latest #}
    
    {#
        beware, this won't be commented out... 
        actually renders as regular body text on the page
    #}
    

    I find this especially helpful for <a href="{% url 'view_name' %}" views that have not been created yet.

    0 讨论(0)
提交回复
热议问题