Pergunta

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

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),
)

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?

Foi útil?

Solução

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$', 'myapp.views.manager_view'),
    (r'^items/(?P<page>[0-9]+)$', 'django.views.generic.list_detail.object_list',
        items_list),
)

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

urlpatterns = patterns('myapp.views',
    (r'^manager$', 'manager_view'),
)

urlpatterns += patterns('django.views.generic',
    (r'^items/(?P<page>[0-9]+)$', 'list_detail.object_list',
        items_list),
)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top