django url参数之前包含url与命名空间 [英] django url parameters before include url with namespace
问题描述
在我的最后一个问题中,我询问了如何在包含urls.py之前获取参数的urls。
Django {%url%}网址参数如:url(r'^ foo /< parameter> / $',include(some.urls))
现在我想使用相同的内容包括urls.py 命名空间。
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'),
)
要获取模板中的网址,我使用:
1。 {%url foo:bar parameter_1 = 1%}或{%url for:bar 1%}
2. {%url foo:bar_detail parameter_1 = 1 parameter_2 = 1%}或{%url foo:bar_detail 1 1 %}
我希望得到url:1. / foo / 1 / bar /
和2. / foo / 1 / bar / 1
但不起作用。
有趣的是,如果我打电话:
1。 {%url foo:bar%}
2. {%url foo:bar_detail parameter_2 = 1%}或{%url foo:bar_detail 1%}
我得到url:
1. / foo /(?P< parameter_1>%5Cd +)/ bar / $ c>和
2. / foo /(?P< parameter_1>%5d +)/ bar / 1
$ b $我的问题:我的代码有错,还是代码对我有用,我想做什么。
我再次搜索并发现故障。在 /django/core/urlresolvers.py
中有一个错误。
我在 https://code.djangoproject.com/ticket/11559#no1 。
要解决问题,您必须将 /django/core/urlresolvers.py
替换为从 https://github.com/django/django/commit/02dcbe3317 。
使用 python manage.py runserver
重新启动def-server后,url标签解析正确。从 {%url foo:bar 1%}
而不是<$ c
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))
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 %}
I expect to get the url: 1. /foo/1/bar/
and 2. /foo/1/bar/1
but it does not work.
Interesting: if I call:
1. {% url foo:bar %}
2. {% url foo:bar_detail parameter_2=1 %} or {% url foo:bar_detail 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.
I searched again and found the fault. There's a bug in the /django/core/urlresolvers.py
.
I found the ticket at https://code.djangoproject.com/ticket/11559#no1.
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.
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/
.
这篇关于django url参数之前包含url与命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!