A flask-style decorator for Django views
Project description
Djecorator
Write Django routes in the same style as Flask.
Installation
pip install djecorator
Usage
views.py
from django.shortcuts import render
from djecorator import Route
route = Route()
@route("/")
def index(request):
return render(request, "index.html")
# With path and name parameters
@route("/about/", name="about_page")
def about(request):
return render(request, "about.html")
# With login required protection
@route("/dashboard/", login_required=True)
def dashboard(request):
# Only accessible to authenticated users
return render(request, "dashboard.html")
urls.py
import views
urlpatterns = [
...
]
urlpatterns += views.route.patterns
Features
- Flask-like routing: Define your routes directly above your view functions
- Prefixing: Add URL and name prefixes to group related routes
- Authentication: Protect routes with Django's
login_requireddecorator - Class-based views: Works with both function-based and class-based views
Route Parameters
The Route class accepts the following parameters:
url_prefix: Prefix added to all URL pathsname_prefix: Prefix added to all URL names
The @route() decorator accepts the following parameters:
path: The URL path (defaults to the function name)name: The URL name (defaults to the function name)login_required: Whether the view requires authentication (defaults to False)
Example with prefixes
# Create a route with prefixes
admin_route = Route(url_prefix="/admin", name_prefix="admin_")
@admin_route("/users/")
def admin_users(request):
# This will be available at /admin/users/
# and have the URL name "admin_users"
return render(request, "admin/users.html")
@admin_route("/settings/", login_required=True)
def admin_settings(request):
# This will be available at /admin/settings/
# and have the URL name "admin_settings"
# and require login
return render(request, "admin/settings.html")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
djecorator-1.2.0.tar.gz
(5.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file djecorator-1.2.0.tar.gz.
File metadata
- Download URL: djecorator-1.2.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
597597a01fe8ebd3604933d06e3edd7a357e6c59f31536f5e515f0bd451bb55f
|
|
| MD5 |
672f7218047975a27d6446a8bc087742
|
|
| BLAKE2b-256 |
4342ee30677f5345e57a249617019985194d5ebf6f22957ba605e931b34a22d2
|
File details
Details for the file djecorator-1.2.0-py2.py3-none-any.whl.
File metadata
- Download URL: djecorator-1.2.0-py2.py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d977a9fa8ed0f8a2c0619be30a4b14a9f7f8da9f540b8b4852539cced475ce1
|
|
| MD5 |
1b147974cf8e44b0544cd3f54f2b9d3b
|
|
| BLAKE2b-256 |
34f61951effb9d30980a197bd50c07059ff9e41d77e74be235d0886265ef9a3d
|