Django视图被调用两次(两次GET请求) [英] Django view getting called twice (double GET request)
问题描述
我正在Django中创建分类信息网站。单视图功能可处理全球清单,城市清单,仅易货全球清单和仅易货城市明智清单。此视图称为 ads
。
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
.
网址格式按以下顺序编写(请注意,每个网址格式都有唯一的名称,尽管它与相同的 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'),
)
问题是,当我点击上面列表中名为 classified_listing
的网址时,函数 ads
被称为两次。即这是我在终端上看到的内容:
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
这意味着处理要加倍。我认为 urls.py
返回第一个匹配的URL模式。我做错了什么?解决此问题的最佳方法是什么?其余所有其他呼叫均按预期方式工作(即仅一次)。
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).
注意:如果我错过了一些东西,请询问更多信息。
了解此类事件的绝佳解释: https://groups.google.com/d/msg/django-users/CRMMYWix_60/KEIkguUcqxYJ
Great explanation to understand these type of occurences: https://groups.google.com/d/msg/django-users/CRMMYWix_60/KEIkguUcqxYJ
推荐答案
此问题与 urls.py
中的url模式如何排序无关。
This issue has nothing to do with how url patterns are ordered in urls.py
.
就像在问题下的注释中指出的那样,这与HTML模板中有问题的资产引用有关。
Like pointed out in the comments under the question, this has to do with problematic asset references in the HTML template.
那是什么意思?
例如,尝试 curl -i http:// localhost:8000 / example />>终端中的output.txt
。然后在您选择的编辑器中打开 output.txt
。现在搜索 href
或 src
属性,其中值 None
(或其他格式不正确的)。这就是创建双重通话的原因之一。那就是我的原因。我删除了这些,重复调用消失了。
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.
这里有一个古老但很相关的文章,关于如何在您的计算机上全面诊断此问题: https://groups.google.com/forum/#!msg/django-users/ CRMMYWix_60 / KEIkguUcqxYJ
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
测试愉快。
这篇关于Django视图被调用两次(两次GET请求)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!