[Python] Django messages being displayed twice

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using Django's message framework, and I have a very odd problem where my messages are being displayed twice in the template, even though {{messages|length}} is 1

    My view

    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.')


    in my template:

    {% block message%}
    {{message.count}}
    {% if messages %}
    {{messages|length}}
    {% for message in messages%}
    <p class="{{message.tags}}">{{message}}</p>
    {% endfor %}
    {% endif %}
    {% endblock %}


    Any ideas?

    Continue reading...

Compartilhe esta Página