django / django/new-features

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

Open
#36 2 comments 18 reactions 0 assignees View on GitHub
URLs
Dominant language
No language data
Stars
188
Forks
7
PR merge metrics
No merged PRs in 30d

Description

### Code of Conduct

- [x] I agree to follow Django's Code of Conduct

### Feature Description

Support registering url routes via @decorator.

```python
@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

Contributor guide

No contributing guide indexed for this repository

Research direction

Read the issue's blog post, especially its “Behind the scenes” section, and review the forum discussion for the proposed trade-offs. Compare the current separation between views.py and urls.py with the requested @view(paths="foo/", name="foo") style. Done means Django supports an optional decorator-based route registration approach without making it the main recommended style.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
backend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.