简体   繁体   English

无法使用{%url%}模板标记正确生成带有捕获参数的URL

[英]Cannot properly generate URL with captured arguments using {% url %} template tag

I'm running into an issue with supplying captured URL parameters through template tags. 通过模板标记提供捕获的URL参数时遇到了问题。 I have a URL dispatch with a captured parameter that hands off to another URL dispatch with include() that does not have any captured parameters: 我有一个带有捕获参数的URL调度,该调度将传递给另一个include()没有任何捕获参数的URL调度:

nodemanager.urls: url(r'^(?P<node_id>\\d+)/rank/', include('ranking.urls')), nodemanager.urls: url(r'^(?P<node_id>\\d+)/rank/', include('ranking.urls')),

ranking.urls: url(r'^setup$', views.setup, name='setup'), rank.urls: url(r'^setup$', views.setup, name='setup'),

I am using {% url 'setup' node_id=node.id %} in my template which creates the error: 我在模板中使用{% url 'setup' node_id=node.id %} ,这会产生错误:

TypeError at /stage1/node/5/rank/setup / stage1 / node / 5 / rank / setup处的TypeError

setup() got an unexpected keyword argument 'node_id' setup()得到了意外的关键字参数'node_id'

If I take out the keyword argument and just use: {% url 'setup' %} , the landing page fails to load and I get the (predictable) error: 如果我删除关键字参数并仅使用: {% url 'setup' %} ,则登录页面无法加载,并且出现了(可预测的)错误:

NoReverseMatch at /stage1/ / stage1 /处的NoReverseMatch

Reverse for 'setup' with arguments '()' and keyword arguments '{}' not found. 找不到带有参数'()'和关键字参数'{}'的'setup'。 1 pattern(s) >tried: ['stage1/node/(?P\\d+)/rank/setup$'] 1个尝试过的模式:['stage1 / node /(?P \\ d +)/ rank / setup $']

I know that I need to supply the parameter for node_id to properly reverse the URL. 我知道我需要为node_id提供参数以正确反转URL。 However, the named url "setup" in my ranking app doesn't take any parameters, but the URL that includes it (in the nodemanager app) does. 但是,我的排名应用程序中的命名url“设置”没有任何参数,但是包含它的URL(在nodemanager应用程序中)却没有任何参数。

How would I properly pass the node_id keyword argument using a template tag that points to stage1/node/5/rank/setup , ie something of the form {% url 'setup' ... %} . 我如何使用指向stage1/node/5/rank/setup的模板标记正确地传递node_id关键字参数,即形式为{% url 'setup' ... %} Can I do this? 我可以这样做吗?

Let me know if I need to post more context code; 让我知道是否需要发布更多上下文代码; I tried to include (what I thought) are the relevant parts. 我试图包括(我认为)是相关的部分。

The original error is not due to the URL reversing, but comes when the view is actually called. 原始错误不是由于URL反向引起的,而是在实际调用视图时发生的。 This is probably because you have not declared it to take a node_id argument. 这可能是因为您尚未声明它采用node_id参数。 It should be like this: 应该是这样的:

def setup(request, node_id):
    ...

Once you've fixed that, the original url tag syntax should work. 解决此问题后,原始的url标签语法应该可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM