我有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.

推荐答案

您的常规表达式没有占位符(这就是您获得NoReverseMatch的原因):

url(r'^depict$', cyclops.django.depict, name="cyclops-depict"),

你可以这样做:

{% url cyclops-depict %}?smiles=CO&width=200&height=200

URLconf search does not include GET or POST parameters

或者,如果您希望使用{%url%}标记,则应该将您的url模式重新构造为类似于

r'^depict/(?P<width>\d+)/(?P<height>\d+)/(?P<smiles>\w+)$' 

然后你可以做类似的事情

{% url cyclops-depict 200 200 "CO" %}

Follow-up:

自定义标记的简单示例:

from django.core.urlresolvers import reverse
from django import template
register = template.Library()

@register.tag(name="myurl")
def myurl(parser, token):
    tokens = token.split_contents()
    return MyUrlNode(tokens[1:])

class MyUrlNode(template.Node):
    def __init__(self, tokens):
        self.tokens = tokens
    def render(self, context):
        url = reverse('cyclops-depict')
        qs = '&'.join([t for t in self.tokens])
        return '?'.join((url,qs))

您可以在模板中使用此标记,如下所示:

{% myurl width=200 height=200 name=SomeName %}

希望它能输出类似于

/depict?width=200&height=200&name=SomeName

Django相关问答推荐

在Django管理中仅显示外键的特定值

如何将Django项目连接到容器化的PostgreSQL数据库?

在生成的表单元素处出现多值DictKeyError

Django Admin:在一个部分中同时显示多个应用程序?

情节主题更改问题

Django通用列表视图与多查询搜索

是否可以在 Django 中重命名应用程序的组名?

Django ORM 查询优化问题

Django 在模型中存储用户图像

如何使用 jQuery 建立 Django 网站

何时在 django 中使用 pre_save、save、post_save?

如何在终端中切换 Python 版本?

如何在 Django 中向 ModelForm 添加外键字段?

Django admin:我可以定义字段顺序吗?

PyMongo vs MongoEngine for Django

CSS 文件中的 Django 媒体 URL

Python 社区里的小马是怎么回事?

具有 2 种语言的 Django 站点

使用 .order_by() 和 .latest() 的 Django 查询

防止 django 管理员转义 html