Answer the question
In order to leave comments, you need to log in
Django. Loading images. MultiValueDictKeyError. What to do?
I'm trying to do an image upload in Django.
I create a class in the model with the FileField field:
class UserProfile(models.Model):
user = models.OneToOneField(User, unique=False)
picture = models.FileField(upload_to='media/images/', blank=True, null=True)
def __unicode__(self):
return unicode(self.picture.name)
class ProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ['picture']
def signup(request, user_id=None, template_name='signup.html'):
...
if request.method == 'POST':
...
profileForm = ProfileForm(request.POST, request.FILES)
if profileForm.is_valid():
print("profileForm is valid!")
print(request.POST)
pic = UserProfile(picture=request.FILES['picture'])
pic = profileForm.save(commit=False)
pic.user = request.user
pic.save()
message = "File %s is uploaded" % pic
print(message)
else:
print("NOT VALID!")
<form action="" method="POST" enctype="multi-part/formdata">
<table>
{{ profileForm.as_table }}
</table>
{% csrf_token %}
<input type="submit" name="submit" value="Submit">
</form>
request.FILES['picture']
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/user/1/edit/
Django Version: 1.6.1
Python Version: 3.3.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "C:\Python33\lib\site-packages\django\core\handlers\base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Django\sandpit\blog\views.py" in signup
147. pic = UserProfile(picture=request.FILES['picture'])
File "C:\Python33\lib\site-packages\django\utils\datastructures.py" in __getitem__
301. raise MultiValueDictKeyError(repr(key))
Exception Type: MultiValueDictKeyError at /user/1/edit/
Exception Value: "'picture'"
<QueryDict: ... , 'picture': ['2.jpeg'], ... >
Answer the question
In order to leave comments, you need to log in
I'll duplicate it just in case:
Precisely, the opposite is true. This type is called multipart/form-data
You don't have the picture key in your request.FILES dictionary. Try like this:
value = request.FILES.get('picture')
if value:
pic = UserProfile(picture=value)
# ...
else:
# ...
how difficult it is for you
class UserProfile(models.Model):
user = models.ForeignKey(User)
picture = models.ImageField(upload_to='images', blank=True, null=True)
def __unicode__(self):
return unicode(self.picture.name)
class ViewSuccess(TemplateView):
template_name = 'view_success.html'
class ViewSignup(CreateView):
template_name = 'view_signup.html'
success_url = reverse_lazy('view_success')
model = UserProfile
url(r'^$',
url(r'^signup/$', ViewSignup.as_view(), name='view_signup'),
url(r'^success/$', ViewSuccess.as_view(), name='view_success'), )
Just suffered too. I don't know how, but this shit code works.
@login_required
def products_edit(request, product):
cont = {}
form = ProductForm()
if request.POST:
form = ProductForm(request.POST, request.FILES)
if form.is_valid():
try:
product = Product.objects.get(slug=product, refract=True)
product.name = form.cleaned_data['name']
product.description = form.cleaned_data['description']
product.price = form.cleaned_data['price']
product.razdel = form.cleaned_data['razdel']
product.refract = False
product.save()
cont['message'] = u'Всё сохранено'
except:
product = form.save()
product.img = request.FILES['img']
cont['message'] = u'Новое изделие добавлено'
else:
try:
product_ed = Product.objects.get(slug=product, refract=True)
cont['product_ed'] = product_ed
cont['form'] = ProductForm(instance=product_ed)
except Product.DoesNotExist:
cont['form'] = form
cont['message'] = u'Не найден такой продукт, будет создан новый'
return render_to_response('admining/product_edit.html', cont, context_instance=RequestContext(request))
Try something like this:
add to the UserProfile class
This construct will allow access to the profile through the profile attribute. For example, user.profile.picture
Then in the view add
if request.POST:
form = UserProfileForm(request.POST, request.FILES, instance=request.user.profile)
if form.is_valid():
form.save()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question