2016-11-12 140 views
1

我试图将文件(特别是图像)上传到数据库。当我尝试POSTprofile_photo.html我的表格(附后),它给了我一个错误说: MultiValueDictKeyError at /user_profile/login/upload_profile_photo/在Django中提交表单时无法正确POST POST

profile_photo.html:

<body> 
<div id="blue_background_with_fibers"> 
    <form class="pull-center white form-signin" role="form" action="" method="POST" enctype="multipart/form-data"> 
     {% csrf_token %} {{ form.as_p }} 
     <button class="aqua button2" type="submit" value="OK">Upload</button> 
    </form> 
</div> 

detail.html使用profile_photo.html :

<div class="modal fade" id="profile" role="dialog"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal">&times;</button> 
       <h4 class="modal-title">Upload Profile Picture</h4> 
      </div> 
      <div class="modal-body"> 
       <form> 
        <!--Insert form here--> 
        <iframe src="upload_profile_photo/" allowTransparency="true" scrolling="yes" frameborder="0" width="560px" height="175px"></iframe> 
       </form> 
      </div> 
      <div class="modal-footer"> 
       <span>Ekho © 2016</span> 
      </div> 
     </div> 
    </div> 
</div> 

我相信我搞乱了我的views.py(sp特别是在EditProfileView类别下)。下面是我的views.py:

class EditProfileView(View): 
form_class = EditProfileForm 

def get(self, request): 
    form = self.form_class(None); 
    return render(request, 'ekho/profile_photo.html', {'form': form}) 

def post(self, request): 
    if not request.user.is_authenticated(): 
     return render(request, 'ekho/login.html') 
    else: 
     form = EditProfileForm(request.POST or None, request.FILES or None) 
     if form.is_valid(): 
      user = form.save(commit=False) 
      user.user = request.user 
      user.profile_photo = request.FILES['profile_photo'] 
      file_type = user.profile_photo.url.split('.')[-1] 
      file_type = file_type.lower() 
      if file_type not in IMAGE_FILE_TYPES: 
       context = { 
        'user': user, 
        'form': form, 
        'error_message': 'Image file must be PNG, JPG, or JPEG', 
       } 
       return render(request, 'ekho/detail.html', context) 
      user.save() 
      return render(request, 'ekho/login.html', {'user': user}) 
     return render(request, 'ekho/detail.html', {"form": form,}) 

Models.py:

from django.db import models 
from django.contrib.auth.models import User 

class UserProfile(models.Model): 
    user = models.OneToOneField(User, related_name='profile'); 
    background = models.FileField(upload_to = 'user_profile/media/', blank=True, null=True); 
    profile = models.FileField(upload_to = 'user_profile/media/', blank=True, null=True); 
    about = models.TextField(default='', blank=True); 
    reviews = models.TextField(default='', blank=True); 

    def __str__(self): 
     return self.user.username 

最后urls.py:

from django.conf.urls import url 
from . import views 

app_name = 'user_profile' 

urlpatterns = [ 
# /user_profile/ 
url(r'^$', views.index, name='index'), 

# /user_profile/username 
url(r'^user_profile/detail/$', views.detail, name='detail'), 

# user_profile/register/ 
url(r'^register/$', views.RegisterFormView.as_view(), name='register'), 

# user_profile/login/ 
url(r'^login/$', views.LoginFormView.as_view(), name='login'), 

url(r'^login/upload_profile_photo/$', views.EditProfileView.as_view(), name='edit_profile') 

]

+0

你会请附上您的堆栈跟踪呢?这将有助于调查哪一行提示您错误 – Enix

+0

检查文件类型的代码应该确实在表单本身的clean_fieldname方法中。 –

+0

@Enix 您可以在此处查看堆栈的空间:http://dpaste.com/1MPBNY4# –

回答

0

这可是是问题 在您试图根据您的模式访问'profile_photo'的views.py文件,该文件应该位于'profile'中当您尝试访问不可用快译通

我想这应该工作

views.py关键ls.py

views.py

user.profile_photo = request.FILES['profile_photo'] 

此错误MultiValueDictKeyError发生

user.profile_photo = request.FILES['profile'] 
+0

感谢您指出这个错误,解决了MultiKeyDictError,但是现在我得到一个新的问题,它无法保存所做的更改。当我在'views.py'中写'user.save()'时会发生这种情况。它返回的具体错误是'UNIQUE约束失败:user_profile_userprofile.user_id' –

0

对于你的第二个问题:UNIQUE constraint failed: user_profile_userprofile.user_id

不能使用form.save检索UserProfile实例。在拨打form.save之后,它将创建另一个用户配置文件对象,因此您将获得UNIQUE constraint error

尝试更换form.save有:

user = User.objects.get(pk=request.user.pk).profile