相关疑难解决方法(0)

如何使用查询args构造Django反向/ url?

我有http://example.com/depict?smiles=CO&width=200&height=200(以及其他几个可选参数)等网址

我的urls.py包含:

urlpatterns = patterns('',
    (r'^$', 'cansmi.index'),
    (r'^cansmi$', 'cansmi.cansmi'),
    url(r'^depict$', cyclops.django.depict, name="cyclops-depict"),
Run Code Online (Sandbox Code Playgroud)

我可以转到该URL并获得构建的200x200 PNG,因此我知道该部分有效.

在我的"cansmi.cansmi"响应的模板中,我想给出一些查询参数的命名模板"cyclops-depict"的URL.我以为我能做到

{%url cyclops-depict smiles = input_smiles width = 200 height = 200%}

其中"input_smiles"是通过表单提交输入模板的.在这种情况下,它是字符串"CO",我认为它将创建一个类似于顶部的URL.

此模板因TemplateSyntaxError而失败:

在渲染时捕获异常:反向'cyclops-depict',带参数'()'和关键字参数'{'smiles':u'CO','height':200,'width':200}'未找到.

这是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): …
Run Code Online (Sandbox Code Playgroud)

django url reverse query-string

29
推荐指数
6
解决办法
3万
查看次数

标签 统计

django ×1

query-string ×1

reverse ×1

url ×1