Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

python - How can I add additional data to Django messages?

I'm trying to integrate this snippet into our Django project:

It's just custom HTML and CSS for messages.

The html looks like this:

<div class="bs-calltoaction bs-calltoaction-success">
    <div class="row">
        <div class="col-md-9 cta-contents">
            <h1 class="cta-title">Its a Call To Action</h1>
            <div class="cta-desc">
                <p>Describe the action here.</p>
                <p>Describe the action here.</p>
                <p>Describe the action here.</p>
            </div>
        </div>
        <div class="col-md-3 cta-button">
            <a href="#" class="btn btn-lg btn-block btn-default">Go for It!</a>
        </div>
     </div>
</div>

So if I want to integrate it with messages framework, I can do:

{% for message in messages %}
    <div class="bs-calltoaction bs-calltoaction-{{ message.tags }}">
        <div class="row">
            <div class="col-md-9 cta-contents">
                {{ message }}
            </div>
            <div class="col-md-3 cta-button">
                <a href="#" class="btn btn-lg btn-block btn-default">Go for It!</a>
            </div>
         </div>
    </div>
{% endfor %}

But I would like to specify header <h1> and list of sub-messages <p> so I need to add to message something like dictionary which can hold additional attributes like {'title':'This is a title','submessages':[1,2,3]} so I can do:

{% for message in messages %}
    <div class="bs-calltoaction bs-calltoaction-{{ message.tags }}">
        <div class="row">
            <div class="col-md-9 cta-contents">
                <h1 class="cta-title">{{ message.title }}</h1>
                <div class="cta-desc">
                    <p>{{ message.submessages.0 }}</p>
                    <p>{{ message.submessages.1 }}</p>
                </div>
            </div>
            <div class="col-md-3 cta-button">
                <a href="#" class="btn btn-lg btn-block btn-default">Go for It!</a>
            </div>
         </div>
    </div>
{% endfor %}

Is it possible using Django messages? Or would you suggest another approach? I can pass such variables into the context but I would like to use messages if possible.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can get certain amount of freedom using the extra_tags messages attribute.

See https://docs.djangoproject.com/en/1.11/ref/contrib/messages/#adding-extra-message-tags

So you could have different extra_tags for different calls to action, e.g.

# views.py
messages.success(request, 'You have signed up', extra_tags='suggest_upgrade')

or:

messages.success(request, 'You have signed up', extra_tags='suggest_share')

and then:

{% for message in messages %}

    {% if 'suggest_upgrade' in message.extra_tags %}
        <h1>Get these extra features</h1>
        <p>info here</p>
    {% elif 'suggest_share' in message.extra_tags %}
        <h1>Share with friends</h1>
        <p>other info here</p>
    {% endif %}

{% endfor %}

You could even pass a submessage as the tag itself although it’s probably not designed for that purpose:

messages.success(request, 'Main message here', extra_tags='submessage here')

and then:

{% for message in messages %}

    <h1>{{ message }}</h1>
    <p>{{ message.extra_tags }}</p>

{% endfor %}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...