2011-03-23 58 views
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 %} 

任何想法?

+0

將'{{message.count}}'更改爲'{{messages.count}}'。你有相同的輸出嗎? – DTing 2011-03-23 22:01:06

回答

2

原來這是一個模板繼承問題。仔細檢查並確保您在兩個不同的模板中沒有相同的塊。

+0

謝謝!這幫助我修復了一個長期存在的bug,顯而易見 – bharling 2016-09-08 14:37:41