无法将图像上传到Django Project,获取Form对象没有属性“保存” [英] Unable to upload image to Django Project, getting Form object has no attribute 'save'

查看:84
本文介绍了无法将图像上传到Django Project,获取Form对象没有属性“保存”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过模板输入的文件上传图像文件。我已经按照所有说明操作,但是在附加文件并单击提交时出现此错误。

I am trying to upload an image file through a file input from a template. I have followed all the instructions but getting this error when attaching the file and clicking on submit.

AttributeError: 'PicUpForm' object has no attribute 'save'

,因此我的图像未上传到指定目录以及记录没有插入我的sqlitedb

and hence my image is not uploading to the specified directory as well as the record is not inserting into my sqlitedb

以下是我使用过的所有必要代码:

FOLLOWING ARE ALL THE NECESSARY CODES I HAVE USED:

视图.py

def add_image(request):
    form = PicUpForm()
    if request.method == "POST":
        form = PicUpForm(data=request.POST, files=request.FILES)
    if form.is_valid():
        form.save()
        return redirect("")
    else:
        return render(request, "sample.html", {"form": form})

forms.py

class PicUpForm(forms.Form):
    class Meta:
        model = PicUpClass
        fields = [model.picture]
    picture = forms.ImageField(label='File')

models.py

def upload_to(instance, filename):
    now = timezone_now()
    base, ext = os.path.splitext(filename)
    ext = ext.lower()
    return f"C:/Users/Aayush/ev_manage/face_detector/static/img/{now:%Y/%m/%Y%m%d%H%M%S}{ext}"



class PicUpClass(models.Model):
    picture = models.ImageField(_("picture"), upload_to=upload_to, blank=True, null=True)

sample.html

{% block content %}
{% load static %}
    <form method="post" action="/picup" enctype="multipart/form-data">
        {% csrf_token %}
        {{ form }}
        <button type="submit">submit</button>
    </form>
{% endblock %}

urls.py

...
path('picup', views.add_image, name='picup'),

我还根据需要在创建模型后运行了makemigrations和migration命令。请帮助我,因为我是Python的新手,非常重要,需要完成此功能

Also i have run the makemigrations and migrate commands after creating the model as required. Please help me as i am a rookie in Python and very importantly need to complete this feature

推荐答案

您的 PicUpForm 不是 ModelForm ,因此不会使用 Meta 。请注意,这些字段应该是字符串列表,因此您应该将表单重写为:

Your PicUpForm is not a ModelForm, hence it will not take the Meta into account at all. Note that the fields should be a list of strings, so you should rewrite the form to:

class PicUpForm(forms.ModelForm):
    class Meta:
        model = PicUpClass
        fields = ['picture']

请注意,在您看来,对于 redirect(..),您需要传递名称,因此:

Note that in your view, for the redirect(..) you need to pass the name of a view, so:

def add_image(request):
    if request.method == 'POST':
        form = PicUpForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            form.save()
            return redirect('name-of-view')
    else:
        form = PicUpForm()
    return render(request, 'sample.html', {'form': form})

在这里,您需要替换 视图名称 ,带有视图的名称。

Here you need to replace name-of-view with the name of a view.

如果视图包含参数,则可以将其作为命名参数传递,例如,此处如果你的 视图具有一个 picture_id 参数(如果它具有另一个参数,那么您当然应该更改该参数的名称):

If the view contains a parameter, you can pass this as a named parameter, for example here if your view has a picture_id parameter (if it has another parameter, then of course you should change the name of the parameter):

def add_image(request):
    if request.method == 'POST':
        form = PicUpForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            picup = form.save()
            return redirect('name-of-view', picture_id=picup.pk)
    else:
        form = PicUpForm()
    return render(request, 'sample.html', {'form': form})

这篇关于无法将图像上传到Django Project,获取Form对象没有属性“保存”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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