2016-11-12 76 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