Othman
Othman

Reputation: 3018

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

Upvotes: 3

Views: 766

Answers (1)

knbk
knbk

Reputation: 53669

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

Upvotes: 4

Related Questions