Asaf
Asaf

Reputation: 8216

Add URLS from a Django App

I'd like to add URLs from a Django app I'm adding within settings.py
I've tried adding urls.py in the hopes it would work but it doesn't
(because it only reads the project/project/urls.py and not project/app/urls.py)

How can I make the app add urls?

Upvotes: 3

Views: 8686

Answers (3)

Yubraj Thapa
Yubraj Thapa

Reputation: 201

First, you need to go to the urls.py file of your project. There you need to import include along with path.

from django.urls import path, include

Add URLs patterns

urlpatterns = [
path('admin/', admin.site.urls),
path('cv/', include('app_cv.urls')),
]

Note: app_cv is my app.
That link will take you to the urls of app_cv. Now also add urls in your app. Inside your app create the file named urls.py. In that file

from django.urls import path
from . import views
urlpatterns = [
path('',views.index)
]

Upvotes: 0

rnevius
rnevius

Reputation: 27112

Don't forget to import include from django.conf.urls. Your in your project urls.py:

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

urlpatterns = patterns('',
                       url(r'^SOME_BASE_URL/', include('APP_NAME.urls')),
                       )

This assumes you have some URLs defined in your app urls.py as well.

For example, for a class-based generic view to appear at /SOME_BASE_URL/, your app's urls.py would look something like the following:

from django.conf.urls import patterns, url

from .views import SomeView

urlpatterns = patterns('',
                       url(r'^$', SomeView.as_view(), name='some_name'),
                       )

Upvotes: 0

samu
samu

Reputation: 3120

Use the include function, like this:

urlpatterns = patterns('',
                       url(r'^some_base_url/', include('your_app.urls')),
                       )

Including other urlconfs is described in the docs.

Upvotes: 4

Related Questions