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 NameTypeDescription
upload-urlstringRequired: URL to POST temporary files to, defaults to reverse of ^{tt9}$.
max-sizenumberRequired: Maximum allowed file size in bytes, defaults to ^{tt10}$.
size-errorstringRequired: Text to display if the file doesn’t meet the size requirements, defaults to ^{tt11}$.
use-backgroundbooleanWhether 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启用管理

应该在管理界面中显示临时文件包装器模型。 默认为“真”。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
垃圾收集Java将最大堆大小(Xmx)设置为物理内存的一部分   java调用getFragmentManager()而不扩展片段   Java将SQL db中的日期格式字符串转换为日期对象以进行比较   java如何通过输入月份和年份的整数来查找特定月份?   java Struts2推送通知/WebSocket交互最佳实践   java为什么spring jdbcTemplate batchUpdate逐行插入   java句柄FileUploadBase。带弹簧2的SizeLimitExceedeException   使用Google App Engine(GAE)生成哈希代码的java   javaspringmysql.com。mysql。希杰。jdbc。例外情况。通信异常:通信链路故障   java当我想构建我的循环视图项目时遇到了一些问题   java小部件列表视图加载视图   安卓 java。伊奥。IOException:损坏的文件描述符   java在从库中拾取文件时设置方向   SFTP中每个文件的java线程   读取数据库时出现安卓 Java内存不足异常   java Swagger不够聪明,无法处理匿名类型(如地图)   java需要了解安卓视图中onClick回调的实现   java从管理面板退出后,如何返回主菜单并进入任何面板?   java高效增长的原子阵列