Django ImageField-AttributeError'PublicMediaStorage'对象没有属性'generate_filename'

时间:2019-11-29 12:05:47

标签: django django-models attributeerror imagefield django-storage

尝试提交带imageField的Django表单时出现标题错误。
以下是相关的代码段:

设置

DEFAULT_FILE_STORAGE = 'newt.storage_backends.PublicMediaStorage'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

newt.storage_backends

    class PublicMediaStorage(ClearableFileInput):
        pass

模型

from django_upload_path.upload_path import auto_cleaned_path

class FoodLogging(models.Model):
   food_image = models.ImageField('Food Image', upload_to=auto_cleaned_path, null=True, blank=True, default="NA")

表格

class FoodLoggingForm(BetterModelForm):

class Meta:
    model = FoodLogging
    fieldsets = (
        ...
        ('Food Logging Report', {
            'fields': ('food_image',),
            'legend': "Food Logging Report",
            'template_name': "fieldset_template.html"
        }),
        ...

查看

class FoodLoggingCreate(PermissionRequiredMixin, CreateView):
    model = FoodLogging
    form_class = FoodLoggingForm
    success_url = reverse_lazy('foodloggings')
    permission_required = ('catalog.add_foodlogging')

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(FoodLoggingCreate, self).form_valid(form)

    def get_form_kwargs(self):
        kwargs = super(FoodLoggingCreate, self).get_form_kwargs()
        kwargs.update({'request': self.request})
        return kwargs

HTML

<input type="file" name="food_image" accept="image/*" id="id_food_image">

跟踪

回溯(最近通话最近):   内部文件“ /Users/me/python3.7/site-packages/django/core/handlers/exception.py”,第34行     响应= get_response(请求)
  _get_response
中的文件“ /Users/me/python3.7/site-packages/django/core/handlers/base.py”,行115     响应= self.process_exception_by_middleware(e,request)
  _get_response
中的文件“ /Users/me/python3.7/site-packages/django/core/handlers/base.py”,第113行     响应= wraped_callback(请求,* callback_args,** callback_kwargs)
  视图中的文件“ /Users/me/python3.7/site-packages/django/views/generic/base.py”,第71行     返回self.dispatch(request,* args,** kwargs)
  派遣文件“ /Users/me/python3.7/site-packages/django/contrib/auth/mixins.py”,行85     return super()。dispatch(request,* args,** kwargs)
  分派中的文件“ /Users/me/python3.7/site-packages/django/views/generic/base.py”,第97行     返回处理程序(请求,* args,** kwargs)
  文件“ /Users/me/python3.7/site-packages/django/views/generic/edit.py”,第194行     return super()。post(request,* args,** kwargs)
  文件“ /Users/me/python3.7/site-packages/django/views/generic/edit.py”,第142行     返回self.form_valid(form)
  文件“ / Users / shahargino / Google Drive / Projects / Newt.v2 / catalog / views.py”,行706,格式为form_valid     返回super(FoodLoggingUpdate,self).form_valid(form)
  文件“ /Users/me/python3.7/site-packages/django/views/generic/edit.py”,行125,格式为form_valid     self.object = form.save()
  保存中的文件“ / Users / shahargino / Google云端硬盘/Projects/Newt.v2/catalog/forms.py”,行566     instance.save()
  保存中的文件“ /Users/me/python3.7/site-packages/django/db/models/base.py”,行741     force_update = force_update,update_fields = update_fields)
  在save_base中的文件“ /Users/me/python3.7/site-packages/django/db/models/base.py”,行779     force_update,使用update_fields,
  _save_table中的文件“ /Users/me/python3.7/site-packages/django/db/models/base.py”,第848行     for non_pks中的f]
  文件“ /Users/me/python3.7/site-packages/django/db/models/base.py”,第848行,位于     for non_pks中的f]
  文件“ /Users/me/python3.7/site-packages/django/db/models/fields/files.py”,行288,在pre_save中     file.save(file.name,file.file,save = False)
  保存中的文件“ /Users/me/python3.7/site-packages/django/db/models/fields/files.py”,第86行     名称= self.field.generate_filename(self.instance,名称)
  在generate_filename中,文件“ /Users/me/python3.7/site-packages/django/db/models/fields/files.py”第307行     返回self.storage.generate_filename(文件名)
  内部文件“ /Users/me/python3.7/site-packages/django/utils/functional.py”,第257行     返回func(self._wrapped,* args) AttributeError:“ PublicMediaStorage”对象没有属性“ generate_filename”

请支持

谢谢,
沙哈尔

0 个答案:

没有答案