Django view is called twice

Sorry for my poor English, I will try to describe my problem as best as possible. I am using Django 1.5 with Python 3.2. I use django.contrib.messages to display messages to users after they are redirected. If I try to load a page redirecting to another page with a message, after several attempts, it does not delete previous messages (only on the last previous page). So, I see two identical messages. This only happens on Google Chrome (and Cromium). Never in FireFox or Opera. Regardless of whether the messages are saved as cookies or in the session. The view function is called twice, but I don’t know why and what exactly has Chrome (???) in it.

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

Added later ...

Ok I started a new project just for this (the "mysite" project 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 %} 

besides all this, I added .py (at and) to my settings:

 # 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 has been done. You can easily reproduce this.

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 at the end: http://www.youtube.com/watch?v=3L27iwP1PqM&feature=youtu.be the quality is not the greatest, but I did paste the code here.

As you can see, the floats are all different, this means that messages are added only during one request and are not displayed on other pages. And this means that, as I understand it, the view function was executed twice, ignoring the first redirect.

PS: This is the same with a session if you save messages manually in the request.session.messages list. I only have this problem when I use Chrome or Chromium. Opera, as you can see, works great.

+4
source share
4 answers

Google Chrome sends a request when you enter the URL. So, when you press enter, it will send another request. The problem you are facing is probably because the time between entering the URL and clicking the button is very short.

Perhaps you can implement a temporary representation as a workaround.

+3
source

This line that calls your views twice:

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

I ran into the same problem ... I printed the entire request and checked the manage.py logs and found that I could render the calls two more times for the css files

To avoid this, I called another function to process the data. Of course you can add

if request.method == 'POST':

to solve this problem.

+1
source

Privet! Try running an application that is not built into the django web server. I don’t know, but maybe I often refresh the page, and biult in the web server does not have time to complete the previous answer.

0
source

I have the same problem, except that I am using Django 1.6, Python 2.7, and I see this problem also in browsers without a browser (Firefox).

I found a connection between this problem and CACHES settings :

Try installing on your production server (Apache):

 CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.dummy.DummyCache', } } 

And make sure you set SESSION_ENGINE :

 SESSION_ENGINE = 'django.contrib.sessions.backends.db' 

In my case, the problem only occurs when CACHES set to the "real cache", for example, MemcachedCache or LocMemCache (the default value if this variable is not set), etc.

0
source

Source: https://habr.com/ru/post/1493887/


All Articles