I have a pretty standard django app, and am wondering how to set the url routing so that I don't have to explicitly map each url to a view.
我有一个非常标准的django应用程序,我想知道如何设置URL路由,以便我不必显式地将每个URL映射到视图。
For example, let's say that I have the following views: Project, Links, Profile, Contact
. I'd rather not have my urlpatterns
look like this:
例如,假设我有以下视图:项目,链接,配置文件,联系人。我宁愿没有我的urlpatterns看起来像这样:
(r'^Project/$', 'mysite.app.views.project'),
(r'^Links/$', 'mysite.app.views.links'),
(r'^Profile/$', 'mysite.app.views.profile'),
(r'^Contact/$', 'mysite.app.views.contact'),
And so on. In Pylons, it would be as simple as:
等等。在Pylons中,它将如此简单:
map.connect(':controller/:action/:id')
And it would automatically grab the right controller and function. Is there something similar in Django?
它会自动抓取正确的控制器和功能。 Django有类似的东西吗?
3 个解决方案
#1
5
mods = ('Project','Links','Profile','Contact')
urlpatterns = patterns('',
*(('^%s/$'%n, 'mysite.app.views.%s'%n.lower()) for n in mods)
)
#2
5
Unless you have a really huge number of views, writing them down explicitly is not too bad, from a style perspective.
除非你有非常多的观点,从风格的角度来看,明确地写下它们并不算太糟糕。
You can shorten your example, though, by using the prefix argument of the patterns
function:
但是,您可以通过使用patterns函数的prefix参数来缩短示例:
urlpatterns = patterns('mysite.app.views',
(r'^Project/$', 'project'),
(r'^Links/$', 'links'),
(r'^Profile/$', 'profile'),
(r'^Contact/$', 'contact'),
)
#3
5
You might be able to use a special view function along these lines:
您可以沿着这些方向使用特殊视图功能:
def router(request, function, module):
m =__import__(module, globals(), locals(), [function.lower()])
try:
return m.__dict__[function.lower()](request)
except KeyError:
raise Http404()
and then a urlconf like this:
然后像这样的urlconf:
(r'^(?P<function>.+)/$', router, {"module": 'mysite.app.views'}),
This code is untested but the general idea should work, even though you should remember:
这段代码未经测试,但总体思路应该有效,即使您应该记住:
Explicit is better than implicit.
显式优于隐式。
#1
5
mods = ('Project','Links','Profile','Contact')
urlpatterns = patterns('',
*(('^%s/$'%n, 'mysite.app.views.%s'%n.lower()) for n in mods)
)
#2
5
Unless you have a really huge number of views, writing them down explicitly is not too bad, from a style perspective.
除非你有非常多的观点,从风格的角度来看,明确地写下它们并不算太糟糕。
You can shorten your example, though, by using the prefix argument of the patterns
function:
但是,您可以通过使用patterns函数的prefix参数来缩短示例:
urlpatterns = patterns('mysite.app.views',
(r'^Project/$', 'project'),
(r'^Links/$', 'links'),
(r'^Profile/$', 'profile'),
(r'^Contact/$', 'contact'),
)
#3
5
You might be able to use a special view function along these lines:
您可以沿着这些方向使用特殊视图功能:
def router(request, function, module):
m =__import__(module, globals(), locals(), [function.lower()])
try:
return m.__dict__[function.lower()](request)
except KeyError:
raise Http404()
and then a urlconf like this:
然后像这样的urlconf:
(r'^(?P<function>.+)/$', router, {"module": 'mysite.app.views'}),
This code is untested but the general idea should work, even though you should remember:
这段代码未经测试,但总体思路应该有效,即使您应该记住:
Explicit is better than implicit.
显式优于隐式。