html格式:
<form action = "{% url 'newcomplaint' %}" class = 'newc' method = "post" enctype = "multipart/form-data" >
{ % csrf_token % }
<div class = "form-group" >
<label for = "fileToUpload" > Add an image(optional) < /label >
<input type = "file" name = "fileToUpload" id = "fileToUpload" >
</div >
<button type = "submit" class = "btn btn-dark bb" > Post New Complaint < /button >
</form >
views.py:
我在这里得到MultiValueDictKeyError at /hood/new
def newcomplaint(request):
if request.method == "POST":
# I dont know how to store a image here
image = request.FILES['fileToupload']
#print(image)
return HttpResponse('works')
model.py
from django.db import models
from django.core.exceptions import ValidationError
from django.contrib.auth.models import User
class Complain(models.Model):
image = models.ImageField(upload_to='images/', blank=True)
我看到的所有解决方案都是使用django表单完成的,我希望避免这种情况,因为我已经制作了一个更大的表单,带有图像字段和一些其他输入(并添加了样式和Js验证检查)
那么如何从表单中检索文件并将其存储在django中
有一个打字错误。你已经写了
fileToupload
。它应该是fileToUpload
代码期望
fileToupload
是键,但是表单使用fileToUpload
我打错了,也找到了解决办法how to store a file/image in django without forms
我的工作代码: views.py
相关问题 更多 >
编程相关推荐