FileField进度条在ModelForm(Django)中上传 [英] Progress bar for FileField upload in a ModelForm (Django)

查看:234
本文介绍了FileField进度条在ModelForm(Django)中上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些指针来实现一个文件上传进度条,提供有关FileField在ModelForm中上传的当前状态的反馈。



我的ModelForm已经有很多字段(不仅FileField),我想通过进度条显示进度的实时反馈。



谢谢。

解决方案

这是我解决问题的方法: django-smartfields ,此应用程序可以处理报告进度,转换图像或视频,清理旧文件等等。它对进度报告有一些要求:





我正在为django-smartfields的文档工作,但这部分应用程序仍然没有记录,但这里是如何使用它。

 从django.db导入模型
from django.conf import settings
from smartfields import fields

class FileModel(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,editable = F $)
file = fields.FileField(uploadTo ='files')
#其他字段....
parent_field_name ='user'

def has_upload_permission(self,user,field_name = None):
return user == self.user

现在在 forms.py



<$ p $来自django的表单
从myapp.models导入
来自crispy_forms.helper的ImportModel
导入FormHelper
来自crispy_forms.layout import Layout
from smartfields.crispy .layout import FileField

class FileForm(forms.ModelForm):

class Meta:
model = FileModel
fields =('file',other字段...)

@property
def file_upload_url(self):
return reverse(smartfields:upload,kwargs = {
'app_label' test_app',
'model':FileModel .__ name__,
'field_name':'file',
'pk':self.instance.pk,
'parent_field_name'用户'
'parent_pk':self。



def __init __(self,* args,** kwargs):
super(FileForm,self).__ init __(* args,** kwargs)
self.helper = FormHelper(self)
self.helper.form_tag = False
self.helper.layout =布局(
FileField('file' ,plupload_options = {
'url':self.file_upload_url,
'filters':{
'max_file_size':20mb,
'mime_types':[{'title' :文件,
'extensions':pdf,doc}]
}}))

在views.py中不需要做任何事情,除了'file_form'应该在上下文中。确保 smartfields INSTALLED_APPS urls 中有code> url(r'^ smartfields /',include('smartfields.urls',namespace ='smartfields')),,或者您可以创建一个自定义的上传视图, code> smartfields.views.FielUpload 。



在模板中:

  {%load static crispy_forms_tags%} 
< link rel =stylesheethref ={%static'bootstrap3 / css / bootstrap.min.css'%} >
< link rel =stylesheethref ={%static'bootstrap3 / css / bootstrap-theme.min.css'%}>
< link rel =stylesheethref ={%static'css / smartfields.css'%}>

< script type =text / javascriptsrc =// ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js\"></脚本>
< script type =text / javascriptsrc ={%static'bootstrap3 / js / bootstrap.min.js'%}>< / script>
< script type =text / javascriptsrc ={%static'plupload / js / plupload.full.min.js'%}>< / script>
< script type =text / javascriptsrc ={%static'js / bootbox.min.js'%}>< / script>
< script type =text / javascriptsrc ={%static'js / smartfields.js'%}>< / script>

< form method =POST>
{%crispy file_form%}
< input class =btn btn-defaulttype =submitname =btn_savevalue =保存>
< / form>

如果您有任何问题或由于某种原因不起作用,请告诉我们。确保您使用github的版本,pypi上的javascript文件已过时。


I'm looking for some pointers to implementing a file upload progress bar that gives feedback about the current status of the upload of a FileField inside a ModelForm.

My ModelForm has got a lot of fields (not only FileField) and I would like to show a live feedback of the progress with a progress bar.

Thanks.

解决方案

Here is my solution to the problem: django-smartfields, this app can take care of reporting progress, convert images or videos, clean up old files and more. It has some requirements for progress reporting:

I am working on documentation for django-smartfields right now, but this portion of the app is still not documented, but here is how you would use it.

from django.db import models
from django.conf import settings
from smartfields import fields

class FileModel(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, editable=False)
    file = fields.FileField(uploadTo='files')
    # other fields ....
    parent_field_name = 'user'

    def has_upload_permission(self, user, field_name=None):
        return user == self.user

Now in forms.py:

from django import forms
from myapp.models import FileModel
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout
from smartfields.crispy.layout import FileField

class FileForm(forms.ModelForm):

    class Meta:
        model = FileModel
        fields = ('file', other fields...)

    @property
    def file_upload_url(self):
        return reverse("smartfields:upload", kwargs={
            'app_label': 'test_app',
            'model': FileModel.__name__,
            'field_name': 'file',
            'pk': self.instance.pk,
            'parent_field_name': 'user'
            'parent_pk': self.instance.user.pk
        })


    def __init__(self, *args, **kwargs):
        super(FileForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.form_tag = False
        self.helper.layout = Layout(
            FileField('file', plupload_options={
                'url': self.file_upload_url,
                'filters': {
                    'max_file_size': "20mb",
                    'mime_types': [{'title': "Documents",
                                    'extensions': "pdf,doc"}]
            }}))

Nothing special needs to be done in the views.py, except that 'file_form' should be in the context. Make sure smartfields are in INSTALLED_APPS and urls have url(r'^smartfields/', include('smartfields.urls', namespace='smartfields')), in them or you can create a customized upload view, by extending smartfields.views.FielUpload.

In the template:

{% load static crispy_forms_tags %}
<link rel="stylesheet" href="{% static 'bootstrap3/css/bootstrap.min.css' %}">
<link rel="stylesheet" href="{% static 'bootstrap3/css/bootstrap-theme.min.css' %}">
<link rel="stylesheet" href="{% static 'css/smartfields.css' %}">

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="{% static 'bootstrap3/js/bootstrap.min.js' %}"></script>
<script type="text/javascript" src="{% static 'plupload/js/plupload.full.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/bootbox.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/smartfields.js' %}"></script>

<form method="POST">
  {% crispy file_form %}
  <input class="btn btn-default" type="submit" name="btn_save" value="Save">
</form>

Let me know if you have any questions or if it doesn't work for some reason. Make sure you use version from github, javascript file on pypi is outdated.

这篇关于FileField进度条在ModelForm(Django)中上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆