Displaying Django Messages Framework Messages

前端 未结 6 564
野趣味
野趣味 2021-01-04 07:12

I have been using the Django Messaging Framework to display messages to a user in the template.

I am outputting them to the template like this:



        
相关标签:
6条回答
  • 2021-01-04 07:53

    I managed with template tags only:

    {% if messages %}
        {% regroup messages by tags as messages %}
        <div id="messages">
        {% for tags in messages %}
            <ul class="{{ tags.grouper }}">
                {% for message in tags.list %}
                    <li>{{ message|capfirst }}</li>
                {% endfor %}
            </ul>
        {% endfor %}
        </div>
    {% endif %}
    

    The key is the {% regroup %} tag.

    This still has an some issues because the tags attribute includes the extra_tags of the message so if you make use of it you will get additional <ul> groups.

    In future versions (probably 1.7), there will be a level_tag attribute so that issue will be gone soon.


    (As soon as the level_tag attribute is available)

    {% if messages %}
        {% regroup messages by level_tag as messages %}
        <div id="messages">
        {% for level in messages %}
            <ul class="{{ level.grouper }}">
                {% for message in level.list %}
                    <li>{{ message|capfirst }}</li>
                {% endfor %}
            </ul>
        {% endfor %}
        </div>
    {% endif %}
    
    0 讨论(0)
  • 2021-01-04 07:55

    A bit of a faff, but you could probably achieve this by adding a custom template context processor (cf. https://docs.djangoproject.com/en/dev/ref/templates/api/ ) -- something like

    def collect_error_messages(request):
        messages = get_messages(request)
        error_messages = [ m for m in messages if 'error' in m.tags]
        return {'error_messages': error_messages}
    

    then add that to your TEMPLATE_CONTEXT_PROCESSORS list in settings.py, and then in templates you can do:

    <ul>
        {% for message in error_messages %}
            <li>{{ message }}</li>
        {% endfor %}
    </ul>
    

    You could do a variation on the same to build a dict mapping error level to message, and then iterate through each dict.

    0 讨论(0)
  • 2021-01-04 07:58

    you can use following to check message tags.

    {% if message.tags == "error" %} your code here {% endif %}

    0 讨论(0)
  • 2021-01-04 08:02

    Of course you can do it with {% regroup %} tag but you have to use dictsort filter aswell if you want to work properly. So firstly, tags should be sort by name and then group:

    {% if messages %}
        {% regroup messages|dictsort:"tags" by tags as message_list  %}
    
        {% for tags in message_list %}
            <div class="alert alert-{{ tags.grouper }}">
                <div class="container">
                    <ul>
                        {% for message in tags.list %}
                            <li>
                                  {{ message }}
                            </li>
                        {% endfor %}
                    </ul>
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                </div>
            </div>
        {% endfor %}
    {% endif %}
    
    0 讨论(0)
  • 2021-01-04 08:07

    You can put an ifequal:

    <ul>
        {% for message in messages.errors %}
            {% if 'error' in message.tags %}<li>{{ message }}</li>{% endif %}
        {% endfor %}
    </ul>
    

    The mapping of message level to message tag can be configured with MESSAGE_TAGS.

    0 讨论(0)
  • 2021-01-04 08:07

    Reto's answer works for me in this way

    {% for message in messages %}
        {% if 'success' in message.tags %}
    
            <div class="alert alert-success">
                <a class="close" href="#" data-dismiss="alert">×</a>
                <strong>Success!</strong>
    
                    {{ message }}
    
            </div>
        {% endif %}
    {% endfor %}
    
    {% for message in messages %}
        {% if 'error' in message.tags %}
            <div class="alert alert-error">
                <a class="close" href="#" data-dismiss="alert">×</a>
                <strong>Error!</strong>
    
                    {{ message }}
    
            </div>
        {% endif %}
    {% endfor %}
    {% for message in messages %}
        {% if 'info' in message.tags %}
            <div class="alert alert-info">
                <a class="close" href="#" data-dismiss="alert">×</a>
                <strong>INFO!</strong>
    
                    {{ message }}
    
            </div>
        {% endif %}
    {% endfor %}
    
    0 讨论(0)
提交回复
热议问题