Last active
October 6, 2017 12:45
-
-
Save yano/ccc0ba167f06e436db7455b550932561 to your computer and use it in GitHub Desktop.
django messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 参考: 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