1
我使用Django的消息框架,我在那裏被模板顯示兩次我的消息很奇怪的問題,即使{{消息|長度}}爲1Django的消息兩次
我鑑於
if request.method == 'POST':
form = EditProfileForm(user=request.user, meta=meta, data=request.POST, files=request.FILES)
if form.is_valid():
user = form.save()
if 'uploaded_image' in request.FILES:
#TODO limit image size, check mime type
filename = request.FILES['uploaded_image']
destination = open('%s/%s' % (settings.FILE_UPLOAD_PATH, form.filename), 'wb+')
for chunk in filename.chunks():
destination.write(chunk)
destination.close()
print 'adding success message' #this is printed once
messages.success(request, 'Settings saved.') #this message is displayed twice
#messages.add_message(request, messages.SUCCESS, 'Yup. Saved.')
return HttpResponseRedirect(reverse('someview'))
else:
print form.errors
messages.error(request, 'Error updating settings. See errors below.')
在我的模板:
{% block message%}
{{message.count}}
{% if messages %}
{{messages|length}}
{% for message in messages%}
<p class="{{message.tags}}">{{message}}</p>
{% endfor %}
{% endif %}
{% endblock %}
任何想法?
將'{{message.count}}'更改爲'{{messages.count}}'。你有相同的輸出嗎? – DTing 2011-03-23 22:01:06