From b27c0f4fb738b614a0a22ff516144b5a0e4f3dbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Wed, 21 Jun 2023 14:00:48 +0200 Subject: [PATCH] wip --- content/django-view-decorator.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/content/django-view-decorator.md b/content/django-view-decorator.md index 1afa8d9..8b3e9a3 100644 --- a/content/django-view-decorator.md +++ b/content/django-view-decorator.md @@ -1,5 +1,5 @@ Title: Bringing Locality of Behaviour to Django Views and URLs -Date: 2023-06-20 +Date: 2023-06-21 Status: hidden Tags: django, views, locality-of-behaviour Slug: bringing-locality-of-behaviour-to-django-views-and-urls @@ -19,7 +19,7 @@ Given a very simple view: :::python # views.py def foo(request: HttpRequest) -> HttpResponse: - return "bar" + return HttpResponse("foo") It is not apparent how to access the view with HTTP. To see what URL the view is tied to we have to look at urls.py: @@ -80,7 +80,7 @@ Then we can use the `view` decorator like so (we dive deeper in what the decorat @view(paths="/foo/", name="foo") def foo(request: HttpRequest) -> HttpResponse: - return "bar" + return HttpResponse("foo") We now have information about how the view is to be accessed right there next to the view itself. @@ -185,11 +185,11 @@ This is where the aptly named `namespaced_decorator_factory` comes into the pict @foo_view(paths="", name="list") def foo_list(request: HttpRequest) -> HttpResponse: - return "foo list" + return HttpResponse("foo list") @foo_view(paths="", name="detail") def foo_detail(request: HttpRequest, id: int) -> HttpResponse: - return "foo detail" + return HttpResponse("foo detail") By calling `namespaced_decorator_factory` we get a specialised decorator for our namespace and we can even provide it with a path which will be prepended to all URLs registered using it. @@ -211,7 +211,7 @@ This opens up a quite nifty possibility of injecting URLs into a namespace from name="custom-view" ) def custom_view(request: HttpRequest) -> HttpResponse: - return "I'm in another namespace" + return HttpResponse("I'm in another namespace") Now we can treat `custom_view` as if it was a part of the `app_1` namespace. Ie. `reverse("app_1:custom-view")` would give us `app_1/my-custom-view/`. Neat!