简体   繁体   中英

'NoneType' object has no attribute 'encode' error in django

I am trying to save the data in database using Python-Django. But i am getting this error:

'userInfo' object has no attribute 'suggestion'        

This is my model.py file model.py

class userInfo(models.Model):
        #user = models.ForeignKey(User)
        #age = models.IntegerField()
        u_name=models.TextField("Name",  null = True, blank = True)
        u_address=models.TextField("Address",  null = True, blank = True)
        def __unicode__(self):
            return self.u_name

This is my view.py file
view.py

    def gaurav(request):
        print request
        form=userInfoForm()
        if request.POST:
            form = userInfoForm(request.POST)
            anw=form.save(commit=False)
            anw.user=request.user
            anw.save()
            form= userInfoForm(request.POST)
            if form.is_valid():
                user1=form.save()
            return render(request, 'userview/home.html', {'form': form})

This is my form.py file
form.py

    class userInfoForm(forms.ModelForm):
        class Meta:
            model = userInfo
        def __init__(self, *args, **kwargs):        
            super(userInfoForm,self).__init__()

Inside the __unicode__ method of userInfo , you are trying to use self.suggestion but suggestion is not defined in the model fields.

Try using another attribute:

class userInfo(models.Model):
    # Model fields..
    def __unicode__(self):
        return self.u_name

如果您已将代码更改为@Sunny Nanda,但仍收到相同的错误,请尝试清除该项目的.pyc文件并重新加载开发服务器

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM