跳转到主要内容

使用装饰器包含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 上的原始工作。

https://badge.fury.io/py/django-decorator-include.svg https://travis-ci.org/twidi/django-decorator-include.svg?branch=develop

安装

假设您已安装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 (5.2 kB 查看散列)

上传时间 源代码

构建发行版

django_decorator_include-3.0-py3-none-any.whl (5.4 kB 查看散列)

上传时间 Python 3

支持者:

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面