如何在应用程序的 urls.py 中设置通用视图?

发布于 2024-10-15 16:59:18 字数 807 浏览 5 评论 0原文

假设我有一个项目的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒芜了季节 2024-10-22 16:59:18

您根本不需要使用前缀 - 您可以为每个 url 指定每个视图的完整路径:

urlpatterns = patterns('',
    (r'^manager

或者,您可以在单个 urlconf 中拥有多个 urlpatterns,并将它们连接起来:

urlpatterns = patterns('myapp.views',
    (r'^manager
, 'myapp.views.manager_view'),
    (r'^items/(?P<page>[0-9]+)

或者,您可以在单个 urlconf 中拥有多个 urlpatterns,并将它们连接起来:


, 'django.views.generic.list_detail.object_list',
        items_list),
)

或者,您可以在单个 urlconf 中拥有多个 urlpatterns,并将它们连接起来:


, 'manager_view'),
)

urlpatterns += patterns('django.views.generic',
    (r'^items/(?P<page>[0-9]+)
, 'myapp.views.manager_view'),
    (r'^items/(?P<page>[0-9]+)

或者,您可以在单个 urlconf 中拥有多个 urlpatterns,并将它们连接起来:


, 'django.views.generic.list_detail.object_list',
        items_list),
)

或者,您可以在单个 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:

urlpatterns = patterns('',
    (r'^manager

Alternatively, you can have multiple urlpatterns in a single urlconf, and concatenate them:

urlpatterns = patterns('myapp.views',
    (r'^manager
, '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:


, 'manager_view'),
)

urlpatterns += patterns('django.views.generic',
    (r'^items/(?P<page>[0-9]+)
, '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:

, '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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文