一个可重用的Django应用,包含一组用于urls、viewsets、commands等的实用工具。
项目描述
utils-plus
一个简单的可重用Django应用,包含各种混合和实用函数。
安装
使用以下命令安装包
pip install django-utils-plus
或使用以下命令安装开发版本
pip install git://github.com/jnoortheen/django-utils-plus.git@master#egg=django-utils-plus
实用工具
管理命令
- clear_records
- create_admin
- test_mail
- cleardata
- create_middleware
模板标签
使用npm提供静态文件
使用package.json方便跟踪项目中的所有外部js
库。它用于保持可用包的最新版本。以下模板标签可以用于在生产环境中从CDN直接提供这些包,在开发期间提供到node_modules
。
unpkg
标准static
模板标签的替代品。当您使用外部静态文件/库(如bootstrap、jquery)时,您可能希望从CDN加载它们而不是在生产环境中自行管理。此标签可以帮助您完成此操作。当settings.DEBUG
为false时,这将返回从package.json
解析的路径,指向版本化的unpkg.com
。否则,它将解析到本地node_modules
。
jsdelivr
like `unpkg` adds support for using https://www.jsdelivr.com/
用法
加载模板标签并像使用static
标签一样使用unpkg
,
{% load static utils_plus_tags %}
<link rel="stylesheet" type="text/css" href="{% unpkg 'bootstrap/dist/css/bootstrap.min.css' %}"/>
<script src="{% unpkg 'bootstrap/dist/js/bootstrap.min.js' %}"></script>
<script src="{% unpkg 'jquery/dist/jquery.min.js' %}"></script>
注意
- package.json应该位于项目的根目录中。
- 当DEBUG为True时,必须安装这些包,并且它们应该已经存在于
node_modules
中。
中间件
- login_required_middleware
轻松处理URL和路由
以优雅且DRY的方式定义urlpatterns。它更容易嵌套多个级别,同时保持可读性。它只是标准url()、include()方法的包装器。
使用Url
后,你的urls.py可能看起来像这样
### urls.py ###
urlpatterns = [
url(r'^studenteditordocument/(?P<doc_pk>\d+)/edit/$', EditView.as_view(), name='edit-student-doc'),
url(r'^studenteditordocument/(?P<doc_pk>\d+)/export/$', ExportView.as_view(), name='export-editore-doc'),
url(r'^docs/$', Docs.as_view(), name='student-documents'),
url(r'^publish/$', PulishOrDelete.as_view(), {'action': 'publish'}, name="publish_document"),
url(r'^delete/$', PulishOrDelete.as_view(), name='delete_document'),
]
### urls.py ###
from utils_plus.router import url
urlpatterns = list(
url('editor')[
url.int('doc_pk')[
url('edit', DocEditorView.as_view(), 'edit-doc'),
url('export', DocExporterView.as_view(), 'export-doc'),
]
]
+ url('docs', Docs.as_view(), 'student-documents')
+ url('publish', DeleteOrPublistDocument.as_view(), 'publish_document', action='publish')
+ url('delete', DeleteOrPublistDocument.as_view(), 'delete_document')
查看tests/test_router.py
了解更多使用案例
模型
-
CheckDeletableModelMixin
添加了一个is_deletable
方法,然后可以在实际删除之前检查任何受影响的关联记录。最初是从这个gist中复制过来的。 -
ChoicesEnum
枚举类,用于与django ORM的选择字段一起使用 -
QueryManager
以更DRY的方式设置select_related、prefetch_related和过滤器到查询集。- 它有一个类似于get_or_create的
first_or_create
方法
- 它有一个类似于get_or_create的
from django.db import models
from utils_plus.models import QueryManager
class Post(models.Model):
author = models.ForeignKey('Author')
comments = models.ManyToManyField('Comment')
published = models.BooleanField()
pub_date = models.DateField()
# custom managers
objects = QueryManager() # equivalent to models.Manager
public_posts = QueryManager(published=True).order_by('-pub_date')
rel_objects = QueryManager().selects('author').prefetches('comments')
配置选项
URL_GROUP_TRAIL_SLASH
- 默认情况下,由该类生成的所有URL都将具有尾随斜杠
- 在settings.py中将此设置为False以更改此行为
视图
- CreateUpdateView:
- 结合CreateView和UpdateView
测试项目
- clone the repo and run migrations after installing dependencies
- `inv test` will run all the test for the app
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分布
django-utils-plus-0.7.4.tar.gz (19.9 kB 查看哈希)
构建分布
django_utils_plus-0.7.4-py3-none-any.whl (24.8 kB 查看哈希)