包含命名空间的 url 之前的 django url 参数 [英] django url parameters before include url with namespace

查看:21
本文介绍了包含命名空间的 url 之前的 django url 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的最后一个问题中,我询问了如何在包含 urls.py 之前让 url 为参数工作,并且它起作用了.Django {% url %} 当 url 带有如下参数时:url(r'^foo//$', include(some.urls))

In my last question I asked how to get urls working for parameter before included urls.py and it worked. Django {% url %} when urls with parameters like: url(r'^foo/<parameter>/$', include(some.urls))

现在我想使用相同的包含 urls.py 与命名空间.

Now I want to use the same included urls.py with namespaces.

urls.py

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

bar.urls.py

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

要在我使用的模板中获取网址:

To get the url in template I use:

1. {% url foo:bar parameter_1=1 %} or {% url for:bar 1 %}
2. {% url foo:bar_detail parameter_1=1 parameter_2=1 %} or {% url foo:bar_detail 1 1 %}

我希望得到 url:1. /foo/1/bar/ 和 2. /foo/1/bar/1 但它不起作用.

I expect to get the url: 1. /foo/1/bar/ and 2. /foo/1/bar/1 but it does not work.

有趣:如果我打电话:

1. {% url foo:bar %}
2. {% url foo:bar_detail parameter_2=1 %} or {% url foo:bar_detail 1 %}

我得到了网址:1. /foo/(?P%5Cd+)/bar/ 和2. /foo/(?P%5d+)/bar/1

I get the urls: 1. /foo/(?P<parameter_1>%5Cd+)/bar/ and 2. /foo/(?P<parameter_1>%5d+)/bar/1

我的问题:我的代码是否有问题,或者代码对我想要做什么没有用处.

My question: Did i have a fault in my code or is the code not useful for, what i want to do.

推荐答案

我再次搜索,发现故障./django/core/urlresolvers.py 中有一个错误.

I searched again and found the fault. There's a bug in the /django/core/urlresolvers.py.

我在 https://code.djangoproject.com/ticket/11559#no1 找到了票.

要解决此问题,您必须将 /django/core/urlresolvers.py 替换为 https://github.com/django/django/commit/02dcbe3317.

To fix the problem you have to replace the /django/core/urlresolvers.py with the changed file from https://github.com/django/django/commit/02dcbe3317.

使用 python manage.py runserver 重新启动 def-server 后,url-tags 解析正确.我从 {% url foo:bar 1 %} 而不是 /foo/(?P%5Cd+) 得到 /foo/1/bar//bar/.

After restarting the def-server with python manage.py runserver the url-tags are resolved correct. I get /foo/1/bar/ from {% url foo:bar 1 %} instead of /foo/(?P<parameter_1>%5Cd+)/bar/.

这篇关于包含命名空间的 url 之前的 django url 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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