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
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:
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.
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 upoutput.txt
in your editor of choice. Now search forhref
orsrc
attributes where values areNone
(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.