简体   繁体   中英

How to have separate urls for the same app in Django 1.9

I have an app that will have 2 kinds of urls, one that will be included in other apps and one will be included in the settings app. I want to have a way to include only part of the urls without creating a seperate file for it.

# records app -- urls.py
urlpatterns = [
    url(r'^create/$', RecordCreate.as_view(), name="record-create"),
    url(r'^(?P<pk>\d+)/update/$', RecordUpdate.as_view(), name="record-update"),
    url(r'^(?P<pk>\d+)/delete/$', RecordDelete.as_view(), name="record-delete"),
]


urlpatterns_types = [
    url(r'^$', RecordTypeList.as_view(), name="record-type-list"),
    url(r'^(?P<pk>\d+)/$', RecordTypeDetail.as_view(), name="record-type-detail"),
    url(r'^create/$', RecordTypeCreate.as_view(), name="record-type-create"),
    url(r'^(?P<pk>\d+)/update/$', RecordTypeUpdate.as_view(), name="record-type-update"),
    url(r'^(?P<pk>\d+)/delete/$', RecordTypeDelete.as_view(), name="record-type-delete"),
]

Now in the settings app I want to include only the urlpatterns_types urls. However I tried to include them but I couldn't

The only way that I found to create separate files and then include them as a module

Here is an example of the expected result

# player app -- urls.py

from django.conf.urls import patterns, include, url
from .views import *

urlpatterns = [

    # Records App Urls
    url(r'^(?P<player_id>\d+)/records/', include('records.urls')),
]



# settings app -- urls.py

from django.conf.urls import patterns, include, url
from .views import *

urlpatterns = [

    # Records App Urls
    url(r'^(?P<player_id>\d+)/records/', include('records.urls.urlpatterns_types')),
]

Project Tree

-- soccer_game
    -- soccer_game
        -- settings.py
        -- urls.py

    -- players
        -- models.py
        -- urls.py
        -- views.py

    -- main_settings
        -- models.py
        -- urls.py
        -- views.py

You can import the module and pass the list of urls itself:

# settings app -- urls.py

from django.conf.urls import patterns, include, url
from records import urls as records_urls
from .views import *

urlpatterns = [
    # Records App Urls
    url(r'^(?P<player_id>\d+)/records/', include(records_urls.urlpatterns_types)),
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM