如何使用Django URL命名空间? [英] How do you use Django URL namespaces?

查看:110
本文介绍了如何使用Django URL命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 Django URL名称空间。但是我找不到任何示例或文档。

I'm trying to get the hang of Django URL namespaces. But I can't find any examples or documentation.

这是我试过的。

urls。 py:

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^foo/', include('sub_urls', namespace='foo', app_name='foo')),
    (r'^bar/', include('sub_urls', namespace='bar', app_name='bar')),            
)

sub_urls .py:

sub_urls.py:

from django.conf.urls.defaults import patterns, url
from views import view1

urlpatterns = patterns('views',
    url(r'^(?P<view_id>\d+)/$', view1, name='view1')
)

views.py:

from django.shortcuts import render_to_response

def view1(request, view_id):
    return render_to_response('view1.html', locals())

在view1.html中,{%url foo: view1 3%}输出/ foo / 3,{%url bar:view1 3%}输出/ bar / 3。我是否可以浏览到/ foo / X或/ bar / X。

In view1.html, {% url foo:view1 3 %} outputs /foo/3, and {% url bar:view1 3 %} outputs /bar/3. This holds true whether I browse to /foo/X or /bar/X.

X,并且分别有{%url view1 3%}输出/ foo / 3或/ bar / 3。

What I want is to be able to browse to /foo/X or /bar/X, and have {% url view1 3 %} output either /foo/3 or /bar/3, respectively.

推荐答案

我认为这在django现在是不可能的。看看这个留言板发布,其中引用了 Ticket 11559 。我认为你试图做同样的事情 - 有效地将一个隐含的参数传递给URL标签。

I think that this isn't possible in django right now. Have a look at this message board post which references Ticket 11559. I think that you're trying to do the same thing - effectively pass an implicit parameter to the URL tag.

另外,假设 sub_urls 来自同一个应用程序,你应该确保在这两种情况下,app_name 是相同的。您只需要更改命名空间。

Also, assuming that sub_urls is from the same app both times you should make sure app_name is the same in both cases. You should only need to change namespace.

这篇关于如何使用Django URL命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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