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

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

问题描述

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



urls.py

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

包含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



$ detail $($)
obj1 = Foo.objects.get(id = parameter)
obj2 = Bar.objects.get( id = parameter2)

现在我尝试使用以下模板获取网址:

  {%url bar-url parameter = 1参数2 = 2%} 

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



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

解决方案

是的,你可以这样得到你的网址: -

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

但请注意,您的网址配置实际上应该是这样的: / p>

urls.py

  urlpatterns = pattern('',
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 ,除非你特别地映射: -



urls.py

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

请注意, $ 表示正则表达式的结尾。


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'),
    )

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 %}

I expect to get: /bar/1/foo/2/

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

解决方案

Yes, you can get your url like this:-

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

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'),
)

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.

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

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