如何在应用程序的 urls.py 中设置通用视图?
假设我有一个项目的 urlconf,其中包括 myapp
的 urlconf:
urlpatterns = patterns('',
(r'^myapp', include(myapp.urls)),
)
和一个定义了一些路由的 myapp/urls.py
:
urlpatterns = patterns('myapp.views',
(r'^manager$', 'manager_view'),
)
我想在 中使用通用视图myapp
(即显示项目列表),但如果我在 myapp/urls.py
中定义它,如下所示:
items_list = {
'queryset': Item.objects.all(),
}
urlpatterns = patterns('myapp.views',
(r'^manager$', 'manager_view'),
(r'^items/(?P<page>[0-9]+)$', 'django.views.generic.list_detail.object_list',
items_list),
)
由于 myapp.views< /代码> 前缀。当然,我可以将通用视图模式放入项目的 urls.py 中,但是为应用程序拥有单独的 urls.py 就不再有意义了。
那么如何在应用程序的 urlconf 中使用通用视图呢?
Let's say I have a project's urlconf which includes myapp
's urlconf:
urlpatterns = patterns('',
(r'^myapp', include(myapp.urls)),
)
and a myapp/urls.py
with some routes defined:
urlpatterns = patterns('myapp.views',
(r'^manager
I want to use generic views in myapp
(i.e. to display an item list), but if I define it in myapp/urls.py
like this:
items_list = {
'queryset': Item.objects.all(),
}
urlpatterns = patterns('myapp.views',
(r'^manager
This won't work because of the myapp.views
prefix. Of course I could put the generic views patterns in the project's urls.py, but then having a separate urls.py for the app wouldn't make sense anymore.
So how can I use generic views in an app's urlconf?
, 'manager_view'),
)
I want to use generic views in myapp
(i.e. to display an item list), but if I define it in myapp/urls.py
like this:
This won't work because of the myapp.views
prefix. Of course I could put the generic views patterns in the project's urls.py, but then having a separate urls.py for the app wouldn't make sense anymore.
So how can I use generic views in an app's urlconf?
, 'manager_view'),
(r'^items/(?P<page>[0-9]+)
This won't work because of the myapp.views
prefix. Of course I could put the generic views patterns in the project's urls.py, but then having a separate urls.py for the app wouldn't make sense anymore.
So how can I use generic views in an app's urlconf?
, 'manager_view'),
)
I want to use generic views in myapp
(i.e. to display an item list), but if I define it in myapp/urls.py
like this:
This won't work because of the myapp.views
prefix. Of course I could put the generic views patterns in the project's urls.py, but then having a separate urls.py for the app wouldn't make sense anymore.
So how can I use generic views in an app's urlconf?
, 'django.views.generic.list_detail.object_list', items_list), )This won't work because of the myapp.views
prefix. Of course I could put the generic views patterns in the project's urls.py, but then having a separate urls.py for the app wouldn't make sense anymore.
So how can I use generic views in an app's urlconf?
, 'manager_view'), )I want to use generic views in myapp
(i.e. to display an item list), but if I define it in myapp/urls.py
like this:
This won't work because of the myapp.views
prefix. Of course I could put the generic views patterns in the project's urls.py, but then having a separate urls.py for the app wouldn't make sense anymore.
So how can I use generic views in an app's urlconf?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您根本不需要使用前缀 - 您可以为每个 url 指定每个视图的完整路径:
或者,您可以在单个 urlconf 中拥有多个 urlpatterns,并将它们连接起来:
, 'list_detail.object_list', items_list), ) , 'myapp.views.manager_view'), (r'^items/(?P<page>[0-9]+)或者,您可以在单个 urlconf 中拥有多个 urlpatterns,并将它们连接起来:
, 'django.views.generic.list_detail.object_list', items_list), )或者,您可以在单个 urlconf 中拥有多个 urlpatterns,并将它们连接起来:
You don't need to use the prefix at all - you can specify the full path to each of your views for each url:
Alternatively, you can have multiple urlpatterns in a single urlconf, and concatenate them:
, 'list_detail.object_list', items_list), ) , 'myapp.views.manager_view'), (r'^items/(?P<page>[0-9]+)Alternatively, you can have multiple urlpatterns in a single urlconf, and concatenate them:
, 'django.views.generic.list_detail.object_list', items_list), )Alternatively, you can have multiple urlpatterns in a single urlconf, and concatenate them: