Giter VIP home page Giter VIP logo

django-view-decorator's Introduction

Django View Decorator

Tests Documentation pre-commit.ci status PyPI - Version PyPI - Python Version


django-view-decorator is decorator aimed at bringing locality of behaviour to the connection between a URL and a view in Django.

Read more about the motivation behind the package in a recent blogpost.

Table of Contents

Installation

pip install django-view-decorator

Usage

Setup the project URLconf to include URLs from django-view-decorator:

# project/urls.py (this is what we point the ROOT_URLCONF setting at)
from django.urls import path

from django_view_decorator import include_view_urls

urlpatterns = [
    path("", include_view_urls()),
]

Use the decorator like so, for:

Function-based views

# foos/views.py
from django_view_decorator import view

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

Class-based views

@view(paths="/foo/", name="foo-list")
class FooList(ListView):
    model = Foo

Development

git clone
cd django-view-decorator
pip install hatch
hatch run tests:cov
hatch run tests:typecheck

License

django-view-decorator is distributed under the terms of the MIT license.

django-view-decorator's People

Contributors

jkaeske avatar pre-commit-ci[bot] avatar valberg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

django-view-decorator's Issues

Check view arguments against url arguments

It would be cool if we could raise a check error if the view function doesn't have arguments matching the arguments given in the url path.

@view(paths="/foo/<int:bar>/", name="foo")
def foo(request):
    return "bar"

would result in a check error because bar isn't provided as an argument.

This would catch errors which would otherwise surface on runtime.

View not detected

Hi,
I am trying out your package, and I think it's an awesome idea. I was playing around with this idea of putting an entire django app into one file, like this:

import sys

from django.conf import settings
from django.core.wsgi import get_wsgi_application
from django.urls import path
from django_view_decorator import include_view_urls

settings.configure(
    DEBUG=True,
    ROOT_URLCONF=__name__,
    SECRET_KEY="don't look me",
    INSTALLED_APPS=["app"],
)

urlpatterns = [
    path("", include_view_urls()),
]

application = get_wsgi_application()

if __name__ == "__main__":
    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

And in the folder app, I have a views.py file with following view:

@view(paths="", name="index")
def index(request):
    return HttpResponse("hello")

But when I start the app with python main.py runserver, the view is not detected and I only see the django startproject view with the rocket.
Why is that? Is there a setting I am missing?

my directory structure looks like this:

.
├── app
│   ├── __init__.py
│   └── views.py
└── main.py

This are my package versions:

Package               Version
--------------------- -------
asgiref               3.7.2
Django                4.2.3
django-view-decorator 0.0.4
gunicorn              21.2.0
packaging             23.1
pip                   22.2.2
setuptools            63.2.0
sqlparse              0.4.4
typing_extensions     4.7.1

Detect duplicate URLs

We should be able to detect if two URLs have the exact same path and throw an error early.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.