django.conf.urls.url Example Code

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

url relies on Django's URL dispatcher functionality and is used for mapping URLs to matching view functions within a Django app.

Example 1 from gadget-board

gadget-board is a Django, Django REST Framework (DRF) and Angular web application that is open source under the Apache2 license.

gadget-board / web / gadget_board_backend / urls.py

from django.contrib import admin
from django.conf.urls import url, include
from rest_framework_nested import routers
from rest_framework_jwt.views import obtain_jwt_token
from rest_framework_jwt.views import refresh_jwt_token

from authentication.views import AccountViewSet
from gadgets.views import GadgetViewSet, GadgetDataViewSet

router = routers.SimpleRouter()
router.register(r'accounts', AccountViewSet)
router.register(r'gadgets', GadgetViewSet)
gadgets_router = routers.NestedSimpleRouter(router, r'gadgets', lookup='gadget')
gadgets_router.register(r'data', GadgetDataViewSet, base_name='gadgets-data')

urlpatterns = [
    url(r'^backend/admin/', admin.site.urls),
    url(r'^backend/api-auth/', include('rest_framework.urls', 
        namespace='rest_framework')),
    url(r'^backend/api-token-auth/', obtain_jwt_token),
    url(r'^backend/api-token-refresh/', refresh_jwt_token),
    url(r'^backend/api/v1/', include(router.urls)),
    url(r'^backend/api/v1/', include(gadgets_router.urls)),
]

Example 2 from wagtail

wagtail (project website) is a fantastic Django-based CMS with code that is open source under the BSD 3-Clause "New" or "Revised" License.

wagtail/wagtail/admin/urls/pages.py

# pages.py
from django.conf.urls import url

from wagtail.admin.views import page_privacy, pages


app_name = 'wagtailadmin_pages'
urlpatterns = [
    url(r'^add/(\w+)/(\w+)/(\d+)/$', pages.create, name='add'),
    url(r'^add/(\w+)/(\w+)/(\d+)/preview/$',
        pages.PreviewOnCreate.as_view(), name='preview_on_add'),
    url(r'^usage/(\w+)/(\w+)/$', pages.content_type_use, 
        name='type_use'),

    url(r'^(\d+)/edit/$', pages.edit, name='edit'),
    url(r'^(\d+)/edit/preview/$', pages.PreviewOnEdit.as_view(), 
        name='preview_on_edit'),

    url(r'^(\d+)/view_draft/$', pages.view_draft, name='view_draft'),
    url(r'^(\d+)/add_subpage/$', pages.add_subpage, name='add_subpage'),
    url(r'^(\d+)/delete/$', pages.delete, name='delete'),
    url(r'^(\d+)/unpublish/$', pages.unpublish, name='unpublish'),

    url(r'^search/$', pages.search, name='search'),

    url(r'^(\d+)/move/$', pages.move_choose_destination, name='move'),
    url(r'^(\d+)/move/(\d+)/$', pages.move_choose_destination, 
        name='move_choose_destination'),
    url(r'^(\d+)/move/(\d+)/confirm/$', pages.move_confirm, 
        name='move_confirm'),
    url(r'^(\d+)/set_position/$', pages.set_page_position, 
        name='set_page_position'),

    url(r'^(\d+)/copy/$', pages.copy, name='copy'),

    url(r'^moderation/(\d+)/approve/$', pages.approve_moderation, 
        name='approve_moderation'),
    url(r'^moderation/(\d+)/reject/$', pages.reject_moderation, 
        name='reject_moderation'),
    url(r'^moderation/(\d+)/preview/$', pages.preview_for_moderation, 
        name='preview_for_moderation'),

    url(r'^(\d+)/privacy/$', page_privacy.set_privacy, 
        name='set_privacy'),

    url(r'^(\d+)/lock/$', pages.lock, name='lock'),
    url(r'^(\d+)/unlock/$', pages.unlock, name='unlock'),

    url(r'^(\d+)/revisions/$', pages.revisions_index, 
        name='revisions_index'),
    url(r'^(\d+)/revisions/(\d+)/view/$', pages.revisions_view, 
        name='revisions_view'),
    url(r'^(\d+)/revisions/(\d+)/revert/$', pages.revisions_revert, 
        name='revisions_revert'),
    url(r'^(\d+)/revisions/(\d+)/unschedule/$', pages.revisions_unschedule, 
        name='revisions_unschedule'),
    url(r'^(\d+)/revisions/compare/(live|earliest|\d+)\.\.\.(live|latest|\d+)/$', 
        pages.revisions_compare, name='revisions_compare'),
]

Example 3 from register

register is a Django, Bootstrap, PostgreSQL project that is open source under the GNU General Public License v3.0. This web application makes it easier for people to register as organ donors. You can see the application live at https://register.organize.org/.

ORGAN-IZE/register/urls.py

from __future__ import unicode_literals

import django.conf
import django.conf.urls
import django.views.generic
import django.contrib.auth.urls
import django.conf
import django.conf.urls.static
import django.conf.urls.i18n
import django.contrib.admin
from django.contrib.auth import views


urlpatterns = [
    django.conf.urls.url(r'^i18n/', 
      django.conf.urls.include('django.conf.urls.i18n')),
    django.conf.urls.url(r'^robots.txt$', 
                         django.views.generic.TemplateView.as_view(\
                           template_name='robots.txt')),
    django.conf.urls.url(r'^', django.conf.urls.include('registration.urls')),
    django.conf.urls.url(r'^brand/', django.conf.urls.include('cobrand.urls')),
    django.conf.urls.url(r'^admin/', django.conf.urls.include(django.contrib.admin.site.urls)),

    # override the admin password reset flow to use the normal site password
    # reset flow
    django.conf.urls.url(r'^password_reset/$', views.password_reset, 
                         name='admin_password_reset'),
    django.conf.urls.url(r'^login/$', 
                         django.views.generic.RedirectView.as_view(url='/admin/login')),
    django.conf.urls.url(r'^', django.conf.urls.include('accountsplus.urls')),
    django.conf.urls.url(r'^widget/', django.conf.urls.include('widget.urls')),
]

## ... the source file continues here without any further examples

Example 4 from django-allauth

django-allauth (project website) is a Django library for easily adding local and social authentication flows to Django projects. It is open source under the MIT License.

django-allauth/allauth/account/urls.py

from . import views

from django.conf.urls import url


urlpatterns = [
    url(r"^signup/$", views.signup, name="account_signup"),
    url(r"^login/$", views.login, name="account_login"),
    url(r"^logout/$", views.logout, name="account_logout"),

    url(r"^password/change/$", views.password_change,
        name="account_change_password"),
    url(r"^password/set/$", views.password_set, 
        name="account_set_password"),

    url(r"^inactive/$", views.account_inactive, name="account_inactive"),

    # E-mail
    url(r"^email/$", views.email, name="account_email"),
    url(r"^confirm-email/$", views.email_verification_sent,
        name="account_email_verification_sent"),
    url(r"^confirm-email/(?P<key>[-:\w]+)/$", views.confirm_email,
        name="account_confirm_email"),

    # password reset
    url(r"^password/reset/$", views.password_reset,
        name="account_reset_password"),
    url(r"^password/reset/done/$", views.password_reset_done,
        name="account_reset_password_done"),
    url(r"^password/reset/key/(?P<uidb36>[0-9A-Za-z]+)-(?P<key>.+)/$",
        views.password_reset_from_key,
        name="account_reset_password_from_key"),
    url(r"^password/reset/key/done/$", 
        views.password_reset_from_key_done,
        name="account_reset_password_from_key_done"),
]

Example 5 from django-cms

django-cms (project website) is a Python-based content management system (CMS) library for use with Django web apps that is open sourced under the BSD 3-Clause "New" License.

django-cms/cms/urls.py

# -*- coding: utf-8 -*-
from django.conf import settings
from django.conf.urls import include, url

from cms import views
from cms.apphook_pool import apphook_pool
from cms.appresolver import get_app_patterns
from cms.constants import SLUG_REGEXP


if settings.APPEND_SLASH:
    regexp = r'^(?P<slug>%s)/$' % SLUG_REGEXP
else:
    regexp = r'^(?P<slug>%s)$' % SLUG_REGEXP

if apphook_pool.get_apphooks():
    # If there are some application urls, use special resolver,
    # so we will have standard reverse support.
    urlpatterns = get_app_patterns()
else:
    urlpatterns = []


urlpatterns.extend([
    url(r'^cms_login/$', views.login, name='cms_login'),
    url(r'^cms_wizard/', include('cms.wizards.urls')),
    url(regexp, views.details, name='pages-details-by-slug'),
    url(r'^$', views.details, {'slug': ''}, name='pages-root'),
])

Example 6 from apiserver

apiserver is a RESTful web API server project built with Django for user management tasks such as registration (with email verification), login, logout and password changes.

apiserver / apps / accounts / urls.py

from django.conf.urls import url, include
from tastypie.api import Api
from apps.accounts.api import UserResource
from apps.accounts import signals

v1_api = Api(api_name='v1')
v1_api.register(UserResource())

urlpatterns = [
    url(r'^api/', include(v1_api.urls)),
    url(r'^reset/(?P<uidb36>[0-9A-Za-z_\-]+)/' + \
         '(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', 
        'reset_confirm', 
         name='password_reset_confirm'
        ),

    url(r'^accounts/password/reset/' + \
         '(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
        'django.contrib.auth.views.password_reset_confirm',
        {'post_reset_redirect': '/accounts/password/done/'},),
]

Example 7 from django-angular

django-angular (project examples website) is a library with helper code to make it easier to use Angular as the front-end to Django projects. The code for django-angular is open source under the MIT license.

django-angular / djng / urls.py

import warnings
from django.urls import reverse
from django.conf.urls import url
from django.http.response import HttpResponsePermanentRedirect


warnings.warn("Reversing URL's using urlpatterns is deprecated. "
              "Please use the middleware instead", 
              DeprecationWarning)


def angular_reverse(request, *args, **kwargs):
    url_name = request.GET.get('djng_url_name')
    url_args = request.GET.getlist('djng_url_args', None)
    url_kwargs = {}

    prefix = 'djng_url_kwarg_'
    for param in request.GET:
        if param.startswith(prefix):
            url_kwargs[param[len(prefix):]] = request.GET[param]

    url = reverse(url_name, args=url_args, kwargs=url_kwargs)
    return HttpResponsePermanentRedirect(url)


urlpatterns = [
    url(r'^reverse/$', angular_reverse),
]
TwilioQuest logo.

Learn more Python in the awesome free TwilioQuest 16-bit adventure game.

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 Code Examples django.urls.path django.conf.urls.url django.contrib.admin django.db.models Model django.dispatch Signal django.db.models.signal django.core.mail.send_mail django.core.mail.messages EmailMessage django.conf settings django.forms django.contrib.admin.sites register django.core.management.base BaseCommand django.utils.timezone django.contrib.auth get_user_model django.contrib.auth.decorators login_required django.core.exceptions ImproperlyConfigured django.utils.html format_html django.contrib.auth.hashers make_password django.http Http404 django.http HttpResponse django.http HttpResponseForbidden django.http HttpResponseBadRequest django.contrib.admin.filters SimpleListFilter django.urls.exceptions NoReverseMatch django.http HttpResponsePermanentRedirect django.http HttpResponseNotModified django.http HttpResponseRedirect django.apps.config AppConfig django.db OperationalError django.forms CharField flask_sqlalchemy.SQLAlchemy Model django.db.models CharField django.db.models TextField django.db.models BooleanField flask request flask redirect django.db.models DateTimeField django.db.models IntegerField django.db.models AutoField django.db.models DateField django.db.models FileField django.db.models SlugField django.template.response TemplateResponse django.template.response SimpleTemplateResponse SQLAlchemy Model Examples django.urls reverse_lazy Flask Core and Extensions Code Examples ...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-2019