我有http://example.com/depict?smiles=CO&width=200&height=200个URL(还有其他几个可选参数)
我的网址.py包含:
urlpatterns = patterns('',
(r'^$', 'cansmi.index'),
(r'^cansmi$', 'cansmi.cansmi'),
url(r'^depict$', cyclops.django.depict, name="cyclops-depict"),
我可以转到那个URL,得到构建的200x200 PNG,所以我知道这部分是有效的.
在来自"cansmi.cansmi"响应的模板中,我想为给定一些查询参数的命名模板"cyclops-description"构造一个URL.我以为我能做到
{%url独眼巨人描绘微笑=输入_微笑宽度=200高度=200%}
其中"input_smiles"是通过表单提交对模板的输入.在本例中,它是字符串"CO",我认为它会创建一个类似顶部的URL.
此模板失败,并出现TemplateSyntaxError:
呈现时捕获到异常:找不到带有参数""()""和关键字参数""{""SMILES"":U""Co",""Height"":200,""Width":200}"的""Cyops-Description""的反向.""
无论在StackOverflow上还是在其他地方,这都是一个相当常见的错误消息.在我发现的每一种情况下,人们都将它们与URL路径regexp中的参数一起使用,这与我将参数放入查询中的情况不同.
这意味着我做错了.我该怎么做才对?也就是说,我想使用模板中的某些内容构建完整的URL,包括路径和查询参数.
作为参考,
% python manage.py shell
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.urlresolvers import reverse
>>> reverse("cyclops-depict", kwargs=dict())
'/depict'
>>> reverse("cyclops-depict", kwargs=dict(smiles="CO"))
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Python/2.6/site-packages/django/core/urlresolvers.py", line 356, in reverse
*args, **kwargs)))
File "/Library/Python/2.6/site-packages/django/core/urlresolvers.py", line 302, in reverse
"arguments '%s' not found." % (lookup_view_s, args, kwargs))
NoReverseMatch: Reverse for 'cyclops-depict' with arguments '()' and keyword arguments '{'smiles': 'CO'}' not found.