提问人:Lukinator 提问时间:11/18/2023 最后编辑:Lukinator 更新时间:11/18/2023 访问量:28
为什么我从 Django imageField 收到“此字段为必填项”错误?[关闭]
Why do I get "This field is required" error from Django imageField? [closed]
问:
我对 Django 的图像字段有疑问。它说图像字段是空的,即使我选择了图像 这是我 models.py:
from django.db import models
class Post(models.Model):
picture = models.ImageField(upload_to="uploads")
这是我 forms.py:
from django import forms
from django.forms import ModelForm
from .models import Post
class PostForm(ModelForm):
class Meta:
model = Post
fields = '__all__'
这是我 views.py:
from django.shortcuts import render, redirect
from .forms import PostForm
def postsite(request):
form = PostForm()
if request.method == 'POST':
form = PostForm(request.POST, request.FILES)
if form.is_valid():
form.save()
else:
print(form.errors)
return redirect('post')
context = {'form':form}
return render(request, 'post.html', context)
这是我的帖子.html:
<form action="" method="POST" enctyppe="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Submit Post">
</form>
在我为 imageField 选择图像并提交表单后,此代码的输出为:print(form.errors)
<ul class="errorlist"><li>picture<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
由于关于此错误的其他讨论,我尝试将其添加到我的表单中:
enctyppe="multipart/form-data"
但是,错误仍然存在。
答:
1赞
rusty_vibess
11/18/2023
#1
表单操作中存在拼写错误。enctyppe=
<form action="" method="POST" enctyppe="multipart/form-data">
应该是:
<form action="" method="POST" enctype="multipart/form-data">
评论