我在使用Django认证的内置函数时遇到错误

时间:2017-12-28 08:38:30

标签: python django python-3.x django-models django-forms

我已经在django中待了一段时间。现在我在django中遇到了内置函数的一些问题。 TypeError:' bool'的错误状态对象不可调用。发生这种错误的原因是语句" print(request.user.is_authenticated())'。  以下是源代码:

def login_page(request):
form = LoginForm(request.POST or None)
#ensure user is logged in or not
print(request.user.is_authenticated())
if form.is_valid():
    print(form.cleaned_data)

return render(request,"auth/login.html",{})

对于LoginForm()由我的文件forms.py

导入
from django import forms

class ContactForm(forms.Form):
   #first will be name which is variable
    fullname = forms.CharField(widget=forms.TextInput
        (attrs={"class":"form-control","placeholder":"Your fullname"}))
    email = forms.EmailField(widget=forms.EmailInput
        (attrs={"class":"form-control","placeholder":"Your Email"}))
    content = forms.CharField(widget=forms.Textarea
        (attrs={"class":"form-control","placeholder":"Your content"}))

    def clean_email(self):
       email = self.cleaned_data.get("email")
       if not "gmail.com" in email:
          raise forms.ValidationError("Email has to be gmail.com")
       #return value of email to be stored
       return email
class LoginForm(forms.Form):
    username = forms.CharField()
    password = forms.CharField()

2 个答案:

答案 0 :(得分:0)

是的,你想做的是:

print(request.user.is_authenticated)

没有()因为它是一个内置函数

答案 1 :(得分:0)