我在stackoverflow上看到了类似的问题,但似乎无法让我的工作。当我有一个用户上传照片时,如果输入留空,Django将抛出一个MultiValueDictKeyError。我的imageField中有空白= True,但它似乎不起作用。
我的应用程序中有以下类文章:
def content_file_name(instance, filename):
return '/'.join(['content', instance.user.username, filename])
class Content(models.Model):
user = models.OneToOneField(User, unique=True)
image1 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
image2 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
image3 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
image4 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
image5 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
image6 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
image7 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
image8 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
terms = models.ImageField(upload_to=content_file_name, null=True, blank=True)
这就是我的views.py看起来的样子:
def register(request):
if request.POST:
content = Content()
content.user = request.user
content.image1 = request.FILES['image1_upload']
content.image3 = request.FILES['image3_upload']
content.image4 = request.FILES['image4_upload']
content.image5 = request.FILES['image5_upload']
content.image6 = request.FILES['image6_upload']
content.image7 = request.FILES['image7_upload']
content.image8 = request.FILES['image8_upload']
content.terms = request.POST.get('terms')
content.save()
return redirect('/portal/register')
try:
gallery = Content.objects.get(user=request.user)
return render(request, 'portal/register.html', {'gallery': gallery})
except ObjectDoesNotExist:
print 'Does Not Exist!'
return render(request, 'portal/register.html')
这是Django抛出的错误:
Request Method: POST
Request URL: http://127.0.0.1:8000/portal/register/
Django Version: 1.8.2
Exception Type: MultiValueDictKeyError
Exception Value: "'image1_upload'"
Exception Location: /Library/Python/2.7/site-packages/django/utils/datastructures.py in __getitem__, line 322
Python Executable: /usr/bin/python
html图像上传的地方:
<div class="container">
<form role="form" method="post" action="." id="js-upload-form" enctype="multipart/form-data">
{% csrf_token %}
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">{{ user.username }}</h1>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#">
<img class="img-responsive" id="image1" src="/media/{{ gallery.image1 }}" alt="">
</a>
<input type="file" name="image1_upload" id="image1_upload" multiple>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#">
<img class="img-responsive" id="image2" src="/media/{{ gallery.image2 }}" alt="">
</a>
<input type="file" name="image2_upload" id="image2_upload" multiple>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#">
<img class="img-responsive" id="image3" src="/media/{{ gallery.image3 }}" alt="">
</a>
<input type="file" name="image3_upload" id="image3_upload" multiple>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#">
<img class="img-responsive" id="image4" src="/media/{{ gallery.image4 }}" alt="">
</a>
<input type="file" name="image4_upload" id="image4_upload" multiple>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#">
<img class="img-responsive" id="image5" src="/media/{{ gallery.image5 }}" alt="">
</a>
<input type="file" name="image5_upload" id="image5_upload" multiple>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#">
<img class="img-responsive" id="image6" src="/media/{{ gallery.image6 }}" alt="">
</a>
<input type="file" name="image6_upload" id="image6_upload" multiple>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#">
<img class="img-responsive" id="image7" src="/media/{{ gallery.image7 }}" alt="">
</a>
<input type="file" name="image7_upload" id="image7_upload" multiple>
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<a class="thumbnail" href="#">
<img class="img-responsive" id="image8" src="/media/{{ gallery.image8 }}" alt="">
</a>
<input type="file" name="image8_upload" id="image8_upload" multiple>
</div>
</div>
答案 0 :(得分:2)
如果表单字段留空,则request.FILES [&#39; form_field&#39;]不存在。 你应该使用更像的东西:
content.image1 = request.FILES.get('image1_upload', None)
这样,如果表单字段不存在,模型字段将设置为None。
您还可以检查文件中是否存在文件字段,但最终会更加详细:
if 'image1_upload' in request.FILES:
content.image1 = request.FILES.image1_upload