2👍
Django comes with a messages
application that helps with this.
In your view, you would add the messages that you need displayed:
from django.contrib import messages
from django.shortcuts import render
def someview(request):
# your normal code
messages.add_message(request, messages.INFO, 'Yeehaw!')
return render(request, 'sometemplate.html')
Notice I didn’t return the message in my view, this is because the messages middleware takes care of this for me. All I have to do is return a RequestContext
, which the render
shortcut does.
In the template:
{% if messages %}
{% for message in messages %}
<div{% if message.tags %} class="alert alert-{{ message.tags }}"{% endif %}>
<a class="close" data-dismiss="alert" href="#">×</a>
{{ message }}
</div>
{% endfor %}
{% endif %}
Usually you would put the above code in one of your base templates that every template inherits from; and that’s it.
0👍
I use a re usable view for displaying confirmation
Re usable view
def bootstrap_confirm(
request,
heading,
message,
yes_color='success',
yes_text='Yes',
cancel_color='danger',
cancel_text='No',
cancel_url='',
box_color='info',
base_name='base.html'):
if request.method == 'POST':
confirm = request.session['confirm_value']
assert request.POST.get('confirm') == confirm, "Error"
return request.POST.get('submit_button') == yes_text
else:
confirm = get_random_string(16)
request.session['confirm_value'] = confirm
return render_to_response('django-helpers/twitter-bootstrap/confirm.html', request, {
'confirm_value': confirm,
'confirm_heading': heading,
'message': message,
'base_name': base_name,
'box_color': box_color,
'yes_color': yes_color,
'yes_text': yes_text,
'cancel_color': cancel_color,
'cancel_text': cancel_text,
'cancel_url': cancel_url,
})
Re usable Template
{% extends base_name %}
{% block main-contents %}
<h2>{{ confirm_heading }}</h2>
<form action="" method="post">
{% csrf_token %}
<input type="hidden" name="confirm" value="{{ confirm_value }}">
<div class="alert alert-{{ box_color|default:"info" }}">
<div>{{ message }}</div>
<br>
<div>
<input type="submit" class="btn btn-{{ yes_color|default:"success" }}" name="submit_button" type="submit" value="{{ yes_text|default:"Yes" }}">
{% if cancel_url %}
<a href="{{ cancel_url }}" class="btn btn-{{ cancel_color|default:"danger" }}">{{ cancel_text|default:"No" }}</a>
{% endif %}
</div>
</div>
</form>
{% endblock %}
View
def confirm(request):
msg = '...'
heading = '...'
op = bootstrap_confirm(request, heading, msg)
if isinstance(op, HttpResponse):
return op
if op == True:
# Implement custom logic
elif op == False:
# Implement custom logic
You can use a similar re usable view to display messages also. This code is from my library django-helpers. I am also interested in knowing more strategies. Correct me if I am wrong ?
- [Answered ]-Django slug, `\w` doesn't detect korean + chinese
- [Answered ]-Can't load image in my django template
- [Answered ]-Django: How to manage groups and permission without using default admin
- [Answered ]-Django websocker error: AttributeError: 'super' object has no attribute 'init'
- [Answered ]-Is this sanitizer vulnerable to XSS?
Source:stackexchange.com