在Django中验证错误后重新提交FileField中的文件

时间:2016-06-02 11:04:42

标签: django django-models

使用FileField,ImageField的Django项目表单。一切都很好,但是当引发ValidationError时,我必须重新选择所有文件和图像。我想通过django中的缓存概念来避免这个问题 但我没有获得当前输出请更正此代码并添加(如果有的话)

-models.py

from __future__ import unicode_literals
from django.db import models
# Create your models here.
from django.db import models
from django.db.models import ImageField
# or if you use sorl-thumbnail
# from sorl.thumbnail.fields import ImageField

import os

def resume_path(instance, filename):
    fn, ext = os.path.splitext(filename)
    return "resumes/{id}{ext}".format(id=instance.title, ext=ext)

class Page(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    image =  ImageField(upload_to=resume_path)

-admin.py

from django.contrib import admin
# Register your models here.
from django.contrib import admin
from file_resubmit.admin import AdminResubmitMixin
from .models import Page

from django.forms import ModelForm
from file_resubmit.admin import AdminResubmitImageWidget, 

AdminResubmitFileWidget
from .models import Page

class PageModelForm(ModelForm):

class Meta:
         model = Page
         widgets = {
             'picture': AdminResubmitImageWidget,
             'file': AdminResubmitFileWidget,
         }
         fields = '__all__'

class PageAdmin(admin.ModelAdmin,AdminResubmitMixin):
    form = PageModelForm

admin.site.register(Page, PageAdmin)

-settings.py

    CACHES = {
             'default': {
                 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
             },
             "file_resubmit": {
                 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
                  "LOCATION": '/home/xxxxxx/Downloads/Python/Projects_Python/pleasecome/tmp/file_resubmit',
                # "LOCATION": os.path.join(BASE_DIR,'tmp/file_resubmit/'),
             },
    }

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'sub',
    'file_resubmit',
]

MIDDLEWARE_CLASSES = [
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.middleware.gzip.GZipMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
]

1 个答案:

答案 0 :(得分:0)

问题可能在于课程PageAdmin

class PageAdmin(admin.ModelAdmin,AdminResubmitMixin):
    form = PageModelForm

根据django-form-resubmit的文档,应该以下列方式之一创建ModelAdmin:

class PageAdmin(admin.ModelAdmin):
    form = PageModelForm

class PageAdmin(AdminResubmitMixin, admin.ModelAdmin):
    pass

您声明父类的顺序很重要,因此您应该遵循官方示例。由于您有自定义表单,因此根本不需要使用AdminResubmitMixin