|
| 1 | +title: django.urls.path Examples |
| 2 | +category: page |
| 3 | +slug: django-urls-path |
| 4 | +sortorder: 5000 |
| 5 | +toc: False |
| 6 | +sidebartitle: django.urls.path Examples |
| 7 | +meta: Python code examples for the path function within the django.urls module of the Django project. |
| 8 | + |
| 9 | + |
| 10 | +# django.urls.path Examples |
| 11 | +The [path](https://github.com/django/django/blob/master/django/urls/conf.py) function |
| 12 | +is contained with the |
| 13 | +[django.urls](https://github.com/django/django/tree/master/django/urls) module within |
| 14 | +the [Django project](/django.html) code base. |
| 15 | + |
| 16 | + |
| 17 | +## Example 1 from gadget-board |
| 18 | +[gadget-board](https://github.com/mik4el/gadget-board) is a [Django](/django.html), |
| 19 | +[Django REST Framework (DRF)](/django-rest-framework-drf.html) and |
| 20 | +[Angular](/angular.html) web application that is open source under the |
| 21 | +[Apache2 license](https://github.com/mik4el/gadget-board/blob/master/LICENSE). |
| 22 | + |
| 23 | +[**gadget-board/web/gadget_board_backend/urls.py**](https://github.com/mik4el/gadget-board/blob/master/web/gadget_board_backend/urls.py) |
| 24 | + |
| 25 | +```python |
| 26 | +from django.conf.urls import url, include |
| 27 | +from django.contrib import admin |
| 28 | +from rest_framework_nested import routers |
| 29 | +from rest_framework_jwt.views import obtain_jwt_token |
| 30 | +from rest_framework_jwt.views import refresh_jwt_token |
| 31 | + |
| 32 | +from authentication.views import AccountViewSet |
| 33 | +from gadgets.views import GadgetViewSet, GadgetDataViewSet |
| 34 | + |
| 35 | +router = routers.SimpleRouter() |
| 36 | +router.register(r'accounts', AccountViewSet) |
| 37 | +router.register(r'gadgets', GadgetViewSet) |
| 38 | +gadgets_router = routers.NestedSimpleRouter(router, r'gadgets', lookup='gadget') |
| 39 | +gadgets_router.register(r'data', GadgetDataViewSet, base_name='gadgets-data') |
| 40 | + |
| 41 | +urlpatterns = [ |
| 42 | +~~ url(r'^backend/admin/', admin.site.urls), |
| 43 | + url(r'^backend/api-auth/', include('rest_framework.urls', namespace='rest_framework')), |
| 44 | + url(r'^backend/api-token-auth/', obtain_jwt_token), |
| 45 | + url(r'^backend/api-token-refresh/', refresh_jwt_token), |
| 46 | + url(r'^backend/api/v1/', include(router.urls)), |
| 47 | + url(r'^backend/api/v1/', include(gadgets_router.urls)), |
| 48 | +] |
| 49 | +``` |
| 50 | + |
| 51 | + |
0 commit comments