From 94c04c1cee4ff7fed2ef4c9533fc578971bd00ac Mon Sep 17 00:00:00 2001 From: Radoslav Georgiev Date: Sun, 27 Oct 2019 16:57:34 +0200 Subject: [PATCH 1/2] Introduce basic urls section --- README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/README.md b/README.md index 3f57a03..9bed915 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Expect often updates as we discuss & decide upon different things. * [An example create API](#an-example-create-api) * [An example update API](#an-example-update-api) * [Nested serializers](#nested-serializers) +- [Urls](#urls) - [Exception Handling](#exception-handling) * [Raising Exceptions in Services / Selectors](#raising-exceptions-in-services--selectors) * [Handle Exceptions in APIs](#handle-exceptions-in-apis) @@ -406,6 +407,42 @@ class Serializer(serializers.Serializer): The implementation of `inline_serializer` can be found in `utils.py` in this repo. +## Urls + +We usually organize our urls the same way we organize our APIs - 1 url per API, meaning 1 url per action. + +A general rule of thumb is to split urls from different domains in their own `domain_patterns` list & include from `urlpatterns`. + +Here's an example with the APIs from above: + +```python +from django.urls import path, include + +from project.education.apis import ( + CourseCreateApi, + CourseUpdateApi, + CourseListApi, + CourseDetailApi, + CourseSpecificActionApi, +) + + +course_patterns = [ + path('', CourseListApi.as_view(), name='list'), + path('/', CourseDetailApi.as_view(), name='detail'), + path('create/', CourseCreateApi.as_view(), name='create'), + path('/update/', CourseUpdateApi.as_view(), name='update'), + path( + '/specific-action/', + CourseSpecificActionApi.as_view(), + name='specific-action' + ), +] + +urlpatterns = [ + path('courses/', include((course_patterns, 'courses'))), +] +``` ## Exception Handling From 0b802c94a3a9ebacee7b2a9cf3053f61081019a0 Mon Sep 17 00:00:00 2001 From: Radoslav Georgiev Date: Sun, 27 Oct 2019 17:00:51 +0200 Subject: [PATCH 2/2] Add one more paragraph for splitting urls --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 9bed915..db9f4de 100644 --- a/README.md +++ b/README.md @@ -444,6 +444,8 @@ urlpatterns = [ ] ``` +**Splitting urls like that can give you the extra flexibility to move separate domain patterns to separate modules**, especially for really big projects, where you'll often have merge conflicts in `urls.py`. + ## Exception Handling ### Raising Exceptions in Services / Selectors