Skip to content

Instantly share code, notes, and snippets.

@yano
Last active October 6, 2017 12:45
Show Gist options
  • Save yano/ccc0ba167f06e436db7455b550932561 to your computer and use it in GitHub Desktop.
Save yano/ccc0ba167f06e436db7455b550932561 to your computer and use it in GitHub Desktop.
django messages
# 参考: http://thinkami.hatenablog.com/entry/2016/02/17/060852
# settings.py
from django.contrib.messages import constants as message_constants
MESSAGE_TAGS = {
message_constants.ERROR: 'alert alert-danger',
}
# view.py
from django.contrib import messages
...
# こんな感じで
messages.error(request, '画像サイズが大きすぎます。最大サイズは 1920x1080[pixel] です。')
messages.success(request, ...
messages.warning(request, ...
messages.debug(request, ...
messages.info(request, ...
# in template html
{% if messages %}
<ul class="messages">
{% for message in messages %}
<p {% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</p>
{% endfor %}
</ul>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment