|
| 1 | +title: django.conf.urls.url Examples |
| 2 | +category: page |
| 3 | +slug: django-conf-urls-url-examples |
| 4 | +sortorder: 5001 |
| 5 | +toc: False |
| 6 | +sidebartitle: django.conf.urls.url Examples |
| 7 | +meta: Python code examples for the url function within the django.conf.urls module of the Django project. |
| 8 | + |
| 9 | + |
| 10 | +# django.conf.urls.url Examples |
| 11 | +The [url](https://github.com/django/django/blob/master/django/conf/urls/__init__.py) |
| 12 | +function is contained with the |
| 13 | +[django.conf.urls](https://github.com/django/django/tree/master/django/conf/urls) |
| 14 | +module within the [Django project](/django.html) code base. |
| 15 | + |
| 16 | +`url` relies on Django's |
| 17 | +[URL dispatcher](https://docs.djangoproject.com/en/dev/topics/http/urls/) |
| 18 | +functionality and is used for mapping URLs to matching view functions within |
| 19 | +a Django app. |
| 20 | + |
| 21 | + |
| 22 | +## Example 1 from gadget-board |
| 23 | +[gadget-board](https://github.com/mik4el/gadget-board) is a [Django](/django.html), |
| 24 | +[Django REST Framework (DRF)](/django-rest-framework-drf.html) and |
| 25 | +[Angular](/angular.html) web application that is open source under the |
| 26 | +[Apache2 license](https://github.com/mik4el/gadget-board/blob/master/LICENSE). |
| 27 | + |
| 28 | +[**gadget-board/web/gadget_board_backend/urls.py**](https://github.com/mik4el/gadget-board/blob/master/web/gadget_board_backend/urls.py) |
| 29 | + |
| 30 | +```python |
| 31 | +from django.contrib import admin |
| 32 | +~~from django.conf.urls import url, include |
| 33 | +from rest_framework_nested import routers |
| 34 | +from rest_framework_jwt.views import obtain_jwt_token |
| 35 | +from rest_framework_jwt.views import refresh_jwt_token |
| 36 | + |
| 37 | +from authentication.views import AccountViewSet |
| 38 | +from gadgets.views import GadgetViewSet, GadgetDataViewSet |
| 39 | + |
| 40 | +router = routers.SimpleRouter() |
| 41 | +router.register(r'accounts', AccountViewSet) |
| 42 | +router.register(r'gadgets', GadgetViewSet) |
| 43 | +gadgets_router = routers.NestedSimpleRouter(router, r'gadgets', lookup='gadget') |
| 44 | +gadgets_router.register(r'data', GadgetDataViewSet, base_name='gadgets-data') |
| 45 | + |
| 46 | +urlpatterns = [ |
| 47 | +~~ url(r'^backend/admin/', admin.site.urls), |
| 48 | +~~ url(r'^backend/api-auth/', include('rest_framework.urls', namespace='rest_framework')), |
| 49 | +~~ url(r'^backend/api-token-auth/', obtain_jwt_token), |
| 50 | +~~ url(r'^backend/api-token-refresh/', refresh_jwt_token), |
| 51 | +~~ url(r'^backend/api/v1/', include(router.urls)), |
| 52 | +~~ url(r'^backend/api/v1/', include(gadgets_router.urls)), |
| 53 | +] |
| 54 | +``` |
| 55 | + |
| 56 | + |
| 57 | +## Example 2 from ORGAN-IZE/register |
| 58 | +[register](https://github.com/ORGAN-IZE/register) is a [Django](/django.html), |
| 59 | +[Bootstrap](/bootstrap.html), [PostgreSQL](/postgresql.html) project that is |
| 60 | +open source under the |
| 61 | +[GNU General Public License v3.0](https://github.com/ORGAN-IZE/register/blob/master/LICENSE). |
| 62 | +This web application makes it easier for people to register as organ donors. |
| 63 | +You can see the application live at |
| 64 | +[https://register.organize.org/](https://register.organize.org/). |
| 65 | + |
| 66 | +[**ORGAN-IZE/register/urls.py**](https://github.com/ORGAN-IZE/register/blob/master/urls.py) |
| 67 | + |
| 68 | +```python |
| 69 | +from __future__ import unicode_literals |
| 70 | + |
| 71 | +~~import django.conf |
| 72 | +~~import django.conf.urls |
| 73 | +import django.views.generic |
| 74 | +import django.contrib.auth.urls |
| 75 | +import django.conf |
| 76 | +import django.conf.urls.static |
| 77 | +import django.conf.urls.i18n |
| 78 | +import django.contrib.admin |
| 79 | +from django.contrib.auth import views |
| 80 | + |
| 81 | + |
| 82 | +urlpatterns = [ |
| 83 | +~~ django.conf.urls.url(r'^i18n/', django.conf.urls.include('django.conf.urls.i18n')), |
| 84 | +~~ django.conf.urls.url(r'^robots.txt$', |
| 85 | +~~ django.views.generic.TemplateView.as_view(template_name='robots.txt')), |
| 86 | +~~ django.conf.urls.url(r'^', django.conf.urls.include('registration.urls')), |
| 87 | +~~ django.conf.urls.url(r'^brand/', django.conf.urls.include('cobrand.urls')), |
| 88 | +~~ django.conf.urls.url(r'^admin/', django.conf.urls.include(django.contrib.admin.site.urls)), |
| 89 | + |
| 90 | + # override the admin password reset flow to use the normal site password |
| 91 | + # reset flow |
| 92 | +~~ django.conf.urls.url(r'^password_reset/$', views.password_reset, |
| 93 | +~~ name='admin_password_reset'), |
| 94 | +~~ django.conf.urls.url(r'^login/$', |
| 95 | +~~ django.views.generic.RedirectView.as_view(url='/admin/login')), |
| 96 | +~~ django.conf.urls.url(r'^', django.conf.urls.include('accountsplus.urls')), |
| 97 | +~~ django.conf.urls.url(r'^widget/', django.conf.urls.include('widget.urls')), |
| 98 | +] |
| 99 | + |
| 100 | +## ... the source file continues here without any further examples |
| 101 | +``` |
| 102 | + |
| 103 | + |
| 104 | +## Example 3 from django-allauth |
| 105 | +[django-allauth](https://github.com/pennersr/django-allauth) |
| 106 | +([project website](https://www.intenct.nl/projects/django-allauth/)) is a |
| 107 | +[Django](/django.html) library for easily adding local and social authentication |
| 108 | +flows to Django projects. It is open source under the |
| 109 | +[MIT License](https://github.com/pennersr/django-allauth/blob/master/LICENSE). |
| 110 | + |
| 111 | +[**django-allauth/allauth/account/urls.py**](https://github.com/pennersr/django-allauth/blob/master/allauth/account/urls.py) |
| 112 | + |
| 113 | +```python |
| 114 | +from . import views |
| 115 | + |
| 116 | +~~from django.conf.urls import url |
| 117 | + |
| 118 | + |
| 119 | +urlpatterns = [ |
| 120 | +~~ url(r"^signup/$", views.signup, name="account_signup"), |
| 121 | +~~ url(r"^login/$", views.login, name="account_login"), |
| 122 | +~~ url(r"^logout/$", views.logout, name="account_logout"), |
| 123 | + |
| 124 | +~~ url(r"^password/change/$", views.password_change, |
| 125 | +~~ name="account_change_password"), |
| 126 | +~~ url(r"^password/set/$", views.password_set, name="account_set_password"), |
| 127 | + |
| 128 | +~~ url(r"^inactive/$", views.account_inactive, name="account_inactive"), |
| 129 | + |
| 130 | + # E-mail |
| 131 | +~~ url(r"^email/$", views.email, name="account_email"), |
| 132 | +~~ url(r"^confirm-email/$", views.email_verification_sent, |
| 133 | +~~ name="account_email_verification_sent"), |
| 134 | +~~ url(r"^confirm-email/(?P<key>[-:\w]+)/$", views.confirm_email, |
| 135 | +~~ name="account_confirm_email"), |
| 136 | + |
| 137 | + # password reset |
| 138 | +~~ url(r"^password/reset/$", views.password_reset, |
| 139 | +~~ name="account_reset_password"), |
| 140 | +~~ url(r"^password/reset/done/$", views.password_reset_done, |
| 141 | +~~ name="account_reset_password_done"), |
| 142 | +~~ url(r"^password/reset/key/(?P<uidb36>[0-9A-Za-z]+)-(?P<key>.+)/$", |
| 143 | +~~ views.password_reset_from_key, |
| 144 | +~~ name="account_reset_password_from_key"), |
| 145 | +~~ url(r"^password/reset/key/done/$", views.password_reset_from_key_done, |
| 146 | +~~ name="account_reset_password_from_key_done"), |
| 147 | +] |
| 148 | +``` |
| 149 | + |
0 commit comments