Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django view called twice

Sorry for my bad English, I'll try to describe my problem as best I can. I'm using Django 1.5 with Python 3.2. I'm using django.contrib.messages for showing messages to users after redirecting them. If I try and load the page that redirects to another page which shows a message, after several tries it doesn't delete previous messages (only of the last previous page). And so, what I see is 2 identical messages. This happens only in Google Chrome (and Cromium). Never in FireFox or Opera. Regardless of whether the messages being saved as cookies or in session. The view function is being called twice, but I don't know why and what Chrome in particular has to do with it(???).

I've recorded video: http://www.youtube.com/watch?v=nXtQ0uj1Hbw&feature=youtu.be.

Added later...

Ok. I've started a new project just for this (project "mysite" containing one application inside it called "test_app"):

contents of mysite/urls.py:

from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
    url(r'^ms', include('test_app.urls', namespace="testapp")),
)

contents of test_app.py

from django.conf.urls import patterns, url


urlpatterns = patterns('test_app.views',
    url(r'/go-away/$', 'go_away', name='go-away'),
    url(r'/come-here/$', 'come_here', name='come-here'),
)

contents of test_app/views.py

from django.shortcuts import render
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.contrib import messages

import random

def go_away(request):

    messages.info(request, 'FORBIDDEN PLACE! Float: %f' % random.random())
    return HttpResponseRedirect(reverse('testapp:come-here'))

def come_here(request):
    return render(request, 'testapp/template.html', {})

contents of testapp/templates/testapp/template.html

{% if messages %}
    <ul>
        {% for message in messages %}
            <li>{{ message }}</li>
        {% endfor %}
    </ul>
{% else %}
    There was no redirection.
{% endif %}

apart from all of that, i added into my settings.py (at the and):

# Context processors
TEMPLATE_CONTEXT_PROCESSORS = {
   'django.contrib.messages.context_processors.messages',
}

configured database (mysite/mysite.db) and even ran the command "python3 manage.py syncdb"

Nothing else was done. You can reproduce that easily.

My software: - Python 3.2 - Django 1.5 - Apache 2.2 with MOD_WSGI compiled from source. - All this under Linux Mint 14.

What you should see in the end: http://www.youtube.com/watch?v=3L27iwP1PqM&feature=youtu.be quality is not the gratest, but i did paste the code here.

As you can see, the floats are all different, that means that the messages are added during only one request and do not show themselves on any other pages. And that means, as I can understand, that the view function was run twice, ignoring first redirect.

P.S.: It is the same with session, if you save you messages manually into the request.session.messages list. I have such a problem only when I use Chrome or Chromium. Opera, as you can see, works perfectly.

like image 367
user2626972 Avatar asked Jul 28 '13 06:07

user2626972


1 Answers

Google Chrome sends a request when you type the url. So, when you hit enter, it sends another request. The problem you are having is probably because the time between typing the url and pressing enter is very short.

You could perhaps implement a time-based view like this to circumvent the problem.

like image 81
GusC Avatar answered Oct 09 '22 20:10

GusC