使用装饰器包含Django URL模式
项目描述
使用装饰器包含Django URL模式。
维护者:Stéphane “Twidi” Angel 和 Jon Dufresne,在 https://github.com/twidi/django-decorator-include 上,基于Jeff Kistler在 https://github.com/jeffkistler/django-decorator-include 上的原始工作。
安装
假设您已安装pip,请运行以下命令从PyPI安装
pip install django-decorator-include
用法
decorator_include 旨在在URL conf中作为 django.conf.urls.include 函数的替代品使用。它几乎与 include 一样工作,但是第一个参数应该是装饰器或装饰器的可迭代对象,以应用于所有包含的视图(如果可迭代,装饰器的顺序是函数应用于视图的顺序)。以下是一个URL conf示例
from django.contrib import admin from django.core.exceptions import PermissionDenied from django.urls import path from django.contrib.auth.decorators import login_required, user_passes_test from decorator_include import decorator_include from mysite.views import index def only_user(username): def check(user): if user.is_authenticated and user.username == username: return True raise PermissionDenied return user_passes_test(check) urlpatterns = [ path('', views.index, name='index'), # will redirect to login page if not authenticated path('secret/', decorator_include(login_required, 'mysite.secret.urls')), # will redirect to login page if not authenticated # will return a 403 http error if the user does not have the "god" username path('admin/', decorator_include([login_required, only_user('god')], admin.site.urls), ]
支持的版本
Django版本 |
Python版本 |
---|---|
2.0 |
3.4, 3.5, 3.6, 3.7 |
2.1 |
3.5, 3.6, 3.7 |
2.2 |
3.5, 3.6, 3.7, 3.8 |
3.0 |
3.6, 3.7, 3.8 |
所有用于旧Django/Python支持的库版本
Django版本 |
Python版本 |
库版本 |
---|---|---|
1.4, 1.5 |
2.6, 2.7 |
1.2 |
1.6 |
2.6, 2.7, 3.2, 3.3 |
1.2 |
1.7 |
2.7, 3.2, 3.3, 3.4 |
1.2 |
1.8 |
2.7, 3.2, 3.3, 3.4, 3.5 |
1.3 |
1.9, 1.10 |
2.7, 3.4, 3.5 |
1.3 |
1.11 |
2.7, 3.4, 3.5, 3.6 |
1.4.x (>=1.4.1,<2) |
2.0 |
3.4, 3.5, 3.6, 3.7 |
3.0 |
2.1 |
3.5, 3.6, 3.7 |
3.0 |
2.2 |
3.5, 3.6, 3.7, 3.8 |
3.0 |
3.0 |
3.6, 3.7, 3.8 |
3.0 |
开发
确保您在一个有效的Python版本的虚拟环境中。
从Github获取源代码
git clone -b develop https://github.com/twidi/django-decorator-include.git
然后进入新创建的django-decorator-include目录,安装所需的几个库
pip install -r requirements.txt
要运行测试,此库提供了一个测试项目,因此您可以通过这种方式启动它们
django-admin test --settings=tests.settings tests
或者简单地启动runtests.sh脚本(它将运行这个确切命令)
./runtests.sh
以develop分支为基础。在git上默认分支应该是develop,前提是你像上面所示在git clone命令中使用了-b develop参数。
在创建pull request时,确保您使用的是正确的基分支(twidi/django-decorator-include on develop)。
项目详情
下载文件
下载适合您平台的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。
源代码发行版
构建发行版
django-decorator-include-3.0.tar.gz的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 18db98e0e48dbb3977ab79b5520dbf5ecbb35f0bced8e15ed62dc66762345856 |
|
MD5 | 03ed2c866dce7bf1161fb9ea0778be22 |
|
BLAKE2b-256 | 4d11d13f07b11b37c7f0a4860e37c2f93387f83343e82376313a22f9dd3bda0c |
django_decorator_include-3.0-py3-none-any.whl的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | fe7a96afb23f66dcbc6227d3c48430e53cc5704eab30841ddb8bd940e5ea8a5a |
|
MD5 | dbb4d8936980517048224548739169b3 |
|
BLAKE2b-256 | f7a9d8e41e2b4dc42ae59a28e4f680909bee9831a848495679d5c1b46b5c1a8e |