Django view getting called twice (double GET request)

北慕城南 提交于 2020-01-13 05:56:52

问题


I'm creating a classifieds website in Django. A single view function handles global listings, city-wise listings, barter-only global listings and barter-only city-wise listings. This view is called ads.

The url patterns are written in the following order (note that each has a unique name although it's tied to the same ads view):

urlpatterns = patterns('',
    url(r'^buy_and_sell/$', ads,name='classified_listing'),
    url(r'^buy_and_sell/barter/$', ads,name='barter_classified_listing'),
    url(r'^buy_and_sell/barter/(?P<city>[\w.@+-]+)/$', ads,name='city_barter_classified_listing'),
    url(r'^buy_and_sell/(?P<city>[\w.@+-]+)/$', ads,name='city_classified_listing'),
)

The problem is that when I hit the url named classified_listing in the list above, the function ads gets called twice. I.e. here's what I see in my terminal:

[14/Jul/2017 14:31:08] "GET /buy_and_sell/ HTTP/1.1" 200 53758 
[14/Jul/2017 14:31:08] "GET /buy_and_sell/None/ HTTP/1.1" 200 32882

This means double the processing. I thought urls.py returns the first url pattern matched. What am I doing wrong and what's the best way to fix this? All other calls work as expected btw (i.e. only once).

Note: Ask for more information in case I've missed something.


Great explanation to understand these type of occurences: https://groups.google.com/d/msg/django-users/CRMMYWix_60/KEIkguUcqxYJ


回答1:


This issue has nothing to do with how url patterns are ordered in urls.py.

Like pointed out in the comments under the question, this has to do with problematic asset references in the HTML template.

What does that mean?

For instance, try curl -i http://localhost:8000/example/ >> output.txt in your terminal. Then open up output.txt in your editor of choice. Now search for href or src attributes where values are None (or otherwise malformed). That's one reason a double call is being created. That was the reason for me. I removed these, and the double call disappeared.

There's this old - but relevant - writeup about how to comprehensively diagnose this problem on your machine here: https://groups.google.com/forum/#!msg/django-users/CRMMYWix_60/KEIkguUcqxYJ

Happy testing.




回答2:


As I can't comment on other answers, just to add for future wanderers that for me the "problem" was in a correctly formed but yet for the browser instructing <iframe src="#"..> tag. On django server the view was rendering twice, once with original request and then again by the hidden iframe element that I used for some of the modal popups later in the page usage.

After emptying the src attribute like <iframe src=""..> a second request is no longer initiated and my modals work fine.

The solution actually is from the link posted already in answers before [https://groups.google.com/forum/#!msg/django-users/CRMMYWix_60/KEIkguUcqxYJ][1] where it is explained:

Note that it's a URI. That means something that is retrieved. Since you've used the value "#fff", that will be interpreted by the browser as a reference to the current page (#fff being an anchor, and not passed to the server). Ergo, a second request is made.

that the iframe src # (anchor) is instructing the browser to load again the same URL, for the iframe element in my case. I indeed had several style elements with #fff colors inside and whatnot, but this wasn't it, as browsers are smart enough to recognize this is not an anchor.

With available tools (browser only) I found to be easy to debug and find these initiation href/src attributes over the Network tab of your browser developer tools - in Chrome is just by clicking the Initiator link of the corresponding row - giving you the exact line from the page source that initiated the request to the same URL.



来源:https://stackoverflow.com/questions/45105767/django-view-getting-called-twice-double-get-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!