Django表单变得无效

Django表单变得无效,django,django-models,django-forms,django-views,pinax,Django,Django Models,Django Forms,Django Views,Pinax,我是Django的新手。搬到新的verison后,我在表单方面遇到了一些麻烦。随后, 1、模型 class UserProfileDetails(models.Model): user = models.OneToOneField(User) profilePicture = models.ImageField('Profile Picture',upload_to='static/ProfilePic/', null=True,blank=True) def __str__(self):

我是Django的新手。搬到新的verison后,我在表单方面遇到了一些麻烦。随后,

1、模型

class UserProfileDetails(models.Model):

user = models.OneToOneField(User)
profilePicture = models.ImageField('Profile Picture',upload_to='static/ProfilePic/', null=True,blank=True)

def __str__(self):
    return self.user.username
2、形式

class imageUploadForm(forms.ModelForm):
class Meta:
    model=  UserProfileDetails
    fields = ['user','profilePicture']
3,最后是查看函数

def upload_pic(request):
current_user = request.user
if request.method == 'POST':
    form = imageUploadForm(request.POST, request.FILES)
    if form.is_valid():
        pic = form.cleaned_data['profilePicture']
        m = UserProfileDetails(user= current_user.id,profilePicture=pic)
        m.save()
    else:
        raise NotImplemented("What if the user doesn't have an associated profile?")

return HttpResponseRedirect(reverse('polls:profile'))
这段代码与Django 1.8一起使用。但是在移植到Django 1.10.4之后,表单变得无效。我认为,问题在于OneToOneField

IMP:此外,我正在使用pinax帐户应用程序进行帐户管理


为什么这个表格变得无效

提交表单时,两个字段(用户和配置文件图片)的输入似乎都不正确。我的猜测是,你没有通过用户发送表单,这意味着它是无效的。所以你只是上传图像

表单字段属性中不需要有“user”,因为您已经通过“request.user”在视图中访问了该属性。因此,请从表单中删除“用户”字段


另外,为了确保它是正确的,请将“user=current\u user.id”更改为“user=current\u user”,这样您就可以将实例与实例匹配,而不是将实例与id匹配。

那么您有什么错误?它会引发“raise NotImplemented”(“如果用户没有关联的配置文件怎么办?”)消息,这意味着表单变得无效!…然后写正确的代码,因为每个人都会给你写正确的代码你说的正确代码是什么意思?它只是一个“如果”语句!否则它将重定向到“polls:profile”。这怎么会是一个错误,它是一个黑客代码(我知道)!现在根据您的代码
if request.method=='POST':
它重定向到
polls:profile
,这是您代码的最后一行。据我所知,它应该是
if form.is\u valid():
。现在我取消删除我的答案,你检查我的代码和你的。然后对你的代码进行更改,如果错误仍然存在,我将删除我的答案,或者尝试更正。好吗?太好了。你发现了。谢谢。正如我在问题中提到的,我在Django 1.8中使用了相同的代码,并且成功了(即使有错误)你知道为什么吗??