Django view getting called twice (double GET request)

懵懂的女人 提交于 2019-12-04 17:21:57

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.

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.

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