django upthor为简单的ajax文件上传提供了django应用程序。
django-upthor的Python项目详细描述
django upthor
django-upthor为简单的ajax文件提供了django应用程序 上传。我们用https://github.com/blueimp/jQuery-File-Upload表示 上传功能。
警告:这还不足以成为一个完整的应用程序,但它正在 在那里
用法
第一步。安装
- pip install django-upthor
现在你有两个选择:
- 如果要加密FQ值,请安装pycrypto。 pip install pycrypto==2.6.1
- 或者,可以通过添加 THOR_DISABLE_FQ_ENCRYPT = True到您的设置文件。
第二步。(django 1.6+)
在“设置”中将“upthor”添加到已安装的应用程序中。py:
INSTALLED_APPS = ( ... "upthor", )
然后:
python manage.py migrate
第三步。在应用程序的模型中使用它。
import os import uuid from django.db import models from upthor import fields as thor_fields def random_upload_path(instance, filename): # Split the uuid into two parts so that we won't run into subdirectory count limits. First part has 3 hex chars, # thus 4k possible values. uuid_hex = uuid.uuid4().hex return os.path.join(uuid_hex[:3], uuid_hex[3:], filename) def post_example_file_link(real_instance, temporary_instance, raw_file): """ A callback called after linking the temporary file with the model. **Warning**: Don't call instances save method from here, cause it will cause an recursion error. @:param real_instance An instance of the model the file is attached to @:param temporary_instance An instance of TemporaryFileWrapper that the form links to. @:param raw_file The raw file that is being uploaded. @:return bool If True, the uploaded temporary file is removed once the linking is complete. """ return True def get_file_image(file_path): """ An optional function that returns the display image html for files after uploading is complete""" return '<i class="fa fa-file"></i>' class ExampleModelWithFile(models.Model): name = models.CharField(max_length=50) file = thor_fields.ThorFileField(upload_to=random_upload_path, allowed_types=['*'], widget=thor_fields.ThorSingleUploadWidget, post_link=post_product_file_link, get_upload_image=get_file_image)
第四步。确保包含表单媒体。
确保在模板中包含表单的媒体文件:
例如,如果表单是您的上下文对象,则添加以下代码 使用上载程序字段的模型窗体。
{{ form.media.css }} {{ form.media.js }}
第五步。将上传URL添加到项目URL。
url(r'', include('upthor.urls')),
第6步。可选材料
临时文件清理
如果要自动清理临时文件,则需要 安装django-cron并添加 upthor.cron.CleanTemporaryFiles到设置中的cron类。
也可以使用management命令手动清理 clean_temporary_files。
自定义上载小部件模板
您可以重写ThorSingleUploadWidget.render_template以返回 您自己的小部件模板,而不是hardcoded one defined in widgets.py。尽管结构(包括 大多数类)必须保持不变,在 .file-upload,可用于自定义行为:
Data Attribute Name | Type | Description |
---|---|---|
upload-url | string | Required: URL to POST temporary files to, defaults to reverse of ^{tt9}$. |
max-size | number | Required: Maximum allowed file size in bytes, defaults to ^{tt10}$. |
size-error | string | Required: Text to display if the file doesn’t meet the size requirements, defaults to ^{tt11}$. |
use-background | boolean | Whether or not to use ^{tt12}$ instead of ^{tt13}$ elements, defaults to false. |
后端
目前它只支持本地文件后端,但我们计划添加其他 当我们达到一个稳定的状态时就结束了。
设置
使用django项目可以自定义以下设置 设置文件。
THOR_UPLOAD_到
存储上载文件的路径。默认为“临时文件”。
THOR\u过期时间
在数据库和磁盘上保存临时文件的时间默认值 至“60*60*24”,例如24小时
thor链接的过期时间
将链接的临时文件保存在数据库和磁盘上的时间。 默认为“60*60*6”,例如6小时。
thor_max_文件大小
上载文件的最大文件大小默认为“2*1024*1024”,例如2 兆字节。
thor_disable_fq_encrypt
禁用fq加密,如果为false,则需要安装pycrypto 因为那是用来加密的默认为“false”。
thor\u启用管理
应该在管理界面中显示临时文件包装器模型。 默认为“真”。