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

Example 3 from drf-action-serializer

drf-action-serializer is an extension for Django REST Framework that makes it easier to configure specific serializers to use based on the client's request action. For example, a list view should have one serializer whereas the detail view would have a different serializer.

The project is open source under the MIT license.

drf-action-serializer / action_serializer / urls.py

from django.contrib import admin
from django.urls import include, path
from rest_framework.routers import DefaultRouter

from .sample_group_viewset import GroupViewSet


router = DefaultRouter()
router.register('auth/group', GroupViewSet)
router.register('auth/groups', GroupViewSet)

urlpatterns = [
    path('api/', include(router.urls)),
    path('admin/doc/', include('django.contrib.admindocs.urls')),
    path('admin/', admin.site.urls),
]
1. Introduction 2. Development Environments 3. Data 4. Web Development 5. Deployment 6. DevOps Changelog What Full Stack Means About the Author Future Directions Page Statuses Django ExtensionsDjango Example Codedjango.apps.config AppConfigdjango.conf settingsdjango.conf.urls.urldjango.contrib.admindjango.contrib.admin.filters SimpleListFilterdjango.contrib.admin.sites registerdjango.contrib.auth.decorators login_requireddjango.contrib.auth get_user_modeldjango.contrib.auth.hashers make_passworddjango.core.exceptions ImproperlyConfigureddjango.core.mail.messages EmailMessagedjango.core.mail.send_maildjango.core.management.base BaseCommanddjango.db.models AutoFielddjango.db.models BooleanFielddjango.db.models CharFielddjango.db.models DateFielddjango.db.models DateTimeFielddjango.db.models FileFielddjango.db.models ForeignKeydjango.db.models GenericIPAddressFielddjango.db.models ImageFielddjango.db.models IntegerFielddjango.db.models Modeldjango.db.models PositiveIntegerFielddjango.db.models PositiveSmallIntegerFielddjango.db.models.signaldjango.db.models SlugFielddjango.db.models SmallIntegerFielddjango.db.models TextFielddjango.db OperationalErrordjango.dispatch Signaldjango.formsdjango.forms BooleanFielddjango.forms CharFielddjango.forms ChoiceFielddjango.forms DateFielddjango.forms DateTimeFielddjango.forms EmailFielddjango.forms IntegerFielddjango.forms TypedChoiceFielddjango.http Http404django.http HttpResponsedjango.http HttpResponseBadRequestdjango.http HttpResponseForbiddendjango.http HttpResponseNotModifieddjango.http HttpResponsePermanentRedirectdjango.http HttpResponseRedirectdjango.template.response SimpleTemplateResponsedjango.template.response TemplateResponsedjango.urls.pathdjango.urls reverse_lazydjango.urls.exceptions NoReverseMatchdjango.urls.exceptions Resolver404django.utils.html format_htmldjango.utils.timezone ...or view the full table of contents.

Full Stack Python

Full Stack Python is an open book that explains concepts in plain language and provides helpful resources for those topics.
Updates via Twitter & Facebook.

Matt Makai 2012-2020