django.urls.path Example Code

The path function is contained with the django.urls module within the Django project code base.

path is used for routing URLs to the appropriate view functions within a Django application using the URL dispatcher.

Example 1 from dccnsys

dccnsys is a conference registration system built with Django. The code is open source under the MIT license.

dccnsys/wwwdccn/wwwdccn/urls.py

from django.conf import settings
from django.conf.urls.static import static
from django.urls import path, include

urlpatterns = [
    path('', include('public_site.urls')),
    path('user/', include('user_site.urls')),
    path('auth/', include('auth_app.urls')),
    path('users/', include('users.urls')),
    path('registration/', include('registration.urls')),
    path('conferences/', include('conferences.urls')),
    path('submissions/', include('submissions.urls')),
    path('chair/', include('chair.urls')),
]

if settings.USE_LOCAL_MEDIA:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

if settings.USE_DEBUG_TOOLBAR:
    import debug_toolbar
    urlpatterns = [
        path('__debug__/', include(debug_toolbar.urls)),

        # For django versions before 2.0:
        # url(r'^__debug__/', include(debug_toolbar.urls)),

    ] + urlpatterns

Example 2 from heritagesites

heritagesites is a Django web application with a MySQL backend that displays UNESCO heritage sites. The project code is open source under the MIT license.

heritagesites/heritagesites/urls.py

# urls.py
from django.urls import path, re_path
from . import views


urlpatterns = [
    path('', views.HomePageView.as_view(), name='home'),
    path('about/', views.AboutPageView.as_view(), name='about'),
    path('countries/', views.CountryAreaListView.as_view(), name='country_area'),
    path('countries/<int:pk>/', views.CountryAreaDetailView.as_view(), 
           name='country_area_detail'),
    path('sites/', views.SiteListView.as_view(), name='sites'),
    path('sites/<int:pk>/', views.SiteDetailView.as_view(), name='site_detail'),

    path('sites/new/', views.SiteCreateView.as_view(), name='site_new'),
    path('sites/<int:pk>/delete/', views.SiteDeleteView.as_view(), name='site_delete'),
    path('sites/<int:pk>/update/', views.SiteUpdateView.as_view(), name='site_update'),

    path('sites/search', views.SiteFilterView.as_view(), name="search")
]

Sign up for a monthly email with Full Stack Python tutorials. No spam ever.


Matt Makai 2012-2019