' NoneType'对象没有属性'用户'

时间:2014-04-15 14:18:09

标签: django python-2.7 django-forms

我正在尝试输入域名(domainNm)和域名(emailVerified)的电子邮件地址,并根据表格modelform通过Tld提交{1}}]。

看来,它没有save()外键(来自当前经过身份验证的用户)

domain.FKtoClient = user_info

我在这里做错了什么?

forms.py

class SubmitDomain(ModelForm):
    domainNm = forms.CharField(initial=u'', label='Enter your domain')
    emailVerified = forms.EmailField(label='Email at Domain')

    class Meta:
        model = Tld #Create form based off Model for Tld
        fields = ['domainNm','emailVerified']

    def save(self, request=None):
        instance = self.cleaned_data
        #domain =  instance["domainNm"])
        domains = Tld.objects.filter(domainNm=instance["domainNm"])
        if len(domains) == 0:
            domain = Tld()
        else:
            domain = domains[0]
        user_info = request.user
        unique_id = int(uuid.uuid4())
        domain.generated_hash = str(unique_id)
        domain.entered_email = instance["emailVerified"]
        domain.domainNm = instance["domainNm"]
        domain.FKtoClient = user_info
        domain.save()

谢谢!

1 个答案:

答案 0 :(得分:0)

def save(self, request=None):

您在None的定义中将request的默认值分配给save,那么当save的调用者未通过实例化请求时会发生什么?

user_info = request.user #request is None here

这会抛出你看到的错误。要缓解此问题,请添加简单的if request is not None或类似语句。

修改

在看到您的views.py后,您正在将request.POST传递给SubmitDomain的{​​{1}}法术方法,您尚未定义。您上面定义了模型的方式,您必须将请求传递给__init__,而不是save(),即

__init__()