Django {% url %} 当 url 带有如下参数时: url(r'^foo/<parameter>/$', include(some.urls)) [英] Django {% url %} when urls with parameters like: url(r'^foo/<parameter>/$', include(some.urls))

查看:21
本文介绍了Django {% url %} 当 url 带有如下参数时: url(r'^foo/<parameter>/$', include(some.urls))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有找到任何解决方案,如何使用以下配置(使用Django1.3)获取模板中的url:

i don't find any solution, how to get the url in template with the following configuration (using Django1.3):

urls.py

urlpatterns = patterns('',
    url(r'^/foo/(?P<parameter>d+)/$', include('bar.urls'), name='foo-url'),
    )

包含的 url-conf:

Included url-conf:

bar.urls.py

urlpatterns = patterns('',
    (r'^/bar/$', 'bar.views.index'),
    url(r'^/bar/(?P<parameter2>d+)/$', 'bar.views.detail', name='bar-url'),
    )

bar.views.py

def detail(request, parameter, parameter2):
    obj1 = Foo.objects.get(id=parameter)
    obj2 = Bar.objects.get(id=parameter2)

现在我尝试通过以下方式获取模板中的网址:

Now I try to get the url in template with:

{% url bar-url parameter=1 parameter2=2 %}

我希望得到:/bar/1/foo/2/

在这种情况下可以使用 {% url %} 吗?

Is it posible to use in this case the {% url %}?

推荐答案

是的,你可以这样获取你的 url:-

Yes, you can get your url like this:-

{% url 'bar-url' 1 2 %}

但请注意,您的 url 配置实际上应该是这样的:-

But note that your url configuration should actually be like this:-

urls.py

urlpatterns = patterns('',
    url(r'^/foo/(?P<parameter>d+)/, include('bar.urls')),
)

bar.urls.py

urlpatterns = patterns('',
    (r'^/bar/$, 'bar.views.index'),
    url(r'^/bar/(?P<parameter2>d+)/$, 'bar.views.detail', name='bar-url'),
)

没有 foo-url 除非你特别映射:-

There is no foo-url unless you specifically map:-

urls.py

urlpatterns = patterns('',
    url(r'^/foo/(?P<parameter>d+)/$, 'another.views.foo', name='foo'),
    url(r'^/foo/(?P<parameter>d+)/, include('bar.urls')),
)

注意 $ 表示正则表达式的结束.

Note that $ means the end of the regular expression.

这篇关于Django {% url %} 当 url 带有如下参数时: url(r'^foo/&lt;parameter&gt;/$', include(some.urls))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆