[英]Django: Reverse lookup with no parameters?
In my template I've got this ajax call: 在我的模板中,我得到了这个ajax调用:
$.ajax({
url: "{% url 'webapps:ajax-test' webapp.slug %}"
...
(note that the value of webapp.slug is 'ajax-test') (请注意,webapp.slug的值为“ ajax-test”)
And my urls.py looks like this: 我的urls.py看起来像这样:
url(r'^ajax/(?P<pk>[a-zA-Z0-9\-]+)/$', views.ajax_test, name='ajax-test')
Which works fine, but if I try to do this without a param like this: 哪个工作正常,但是如果我尝试在没有这样的参数的情况下执行此操作:
url(r'^ajax/ajax-test/$', views.ajax_test, name='ajax-test')
I get a NoReverseMatch error. 我收到一个NoReverseMatch错误。 In this situation, I don't need a param because each ajax view is going to be a standalone view. 在这种情况下,我不需要参数,因为每个ajax视图都将成为独立视图。 Is there a cleaner way to do this without putting in a 'dummy' param? 有没有一种更清洁的方式来执行此操作而无需放入“虚拟”参数?
Ok, simple solution, I need to not pass a param in my url template tag like so: 好的,简单的解决方案,我不需要在URL模板标记中传递参数,如下所示:
$.ajax({
url: "{% url 'webapps:ajax-test' %}"
...
Duh. 咄。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.