我尝试通过以下方式创建个人资料图片上传字段:
class SignUpForm(forms.Form):
username = forms.CharField(max_length=30, \
error_messages = {'required': 'Need a username'})
email = forms.EmailField(max_length=30, \
error_messages = {'required': 'Need an email', 'invalid': 'Email is invalid'})
profile_image = forms.ImageField(error_messages = {'required': 'Please select a profile image'})
并在views.py中加载数据:
if request.method != 'POST':
return HttpResponseServerError("Only POST requests allowed.")
form = SignUpForm(request.POST, request.FILES)
logger.info(request.FILES) # this always prints empty '{}'
形式是:
form(enctype="multipart/form-data", method='post')
label(for='username') Username
input#name(name='username', type='text')
label(for='email') Email
input#email(name='email', type='email')
label(for='profile_image') Profile Image
input#profile_image(name='profile_image', type='file')
我提交数据的方式是在js中:
$('form').submit(function(e){
e.preventDefault();
$("#signup-errors").hide();
var form = $(e.target);
var post = $.post('/user/create/', form.serialize(), redirectIfSuccess);
post.fail(function(xhr, ajaxOptions, thrownError) {
$("#signup-errors").html(xhr.getResponseHeader("errors")).show();
});
});
有人可以提示我可能出了什么问题吗?我已经搜索并遵循了这些步骤,但这些文件从未通过... :(
谢谢!