我試圖將文件(特別是圖像)上傳到數據庫。當我嘗試POST
在profile_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">×</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')
]
你會請附上您的堆棧跟蹤呢?這將有助於調查哪一行提示您錯誤 – Enix
檢查文件類型的代碼應該確實在表單本身的clean_fieldname方法中。 –
@Enix 您可以在此處查看堆棧的空間:http://dpaste.com/1MPBNY4# –