Skip to content

Support registering url routes via @decorator. (Locality of Behaviour) #36

Open
@collinanderson

Description

@collinanderson

Code of Conduct

  • I agree to follow Django's Code of Conduct

Feature Description

Support registering url routes via @decorator.

@view(paths="foo/", name="foo")
def foo(request: HttpRequest) -> HttpResponse:
    return HttpResponse("foo")

This blog post describes it better than I can: https://valberg.dk/bringing-locality-of-behaviour-to-django-views-and-urls.html

Forum post discussion here: https://forum.djangoproject.com/t/bringing-locality-of-behaviour-to-django-views-and-urls/21765

Problem

New Django projects can be complicated and have lots of files, with code split between views.py and urls.py. It would be nice for beginners and simple projects to avoid needing a urls.py file and do all url registering in the views.py file itself.

Some people also consider this approach to be more DRY (don't repeat yourself).

Request or proposal

request

Additional Details

It shouldn't be the main recommended approach, as there are clearly disadvantages discussed in the forum post. But rather this should be an alternative option for for those who prefer this style.

Flask, FastAPI, and Django Ninja all use this approach, so it would be nice to have this method available for those who are more comfortable working with that style.

I think it could also allow for a less "heavyweight" project style by not needing a separate urls.py.

Implementation Suggestions

The "Behind the scenes" section of the blog post has some implementation details. https://valberg.dk/bringing-locality-of-behaviour-to-django-views-and-urls.html#behind-the-scenes

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Idea

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions