【问题标题】:How to get Django form method to not 500 when debug is false on raising a form error?在引发表单错误时调试为假时,如何使 Django 表单方法不为 500?
【发布时间】:2013-06-23 01:26:54
【问题描述】:

我的登录代码如下:

class LoginForm(forms.Form):
    email = forms.EmailField(max_length = 254, min_length = 6)
    password = forms.CharField(min_length = 8, widget = forms.PasswordInput())

    def Login(self):
        email = self.cleaned_data.get('email')
        password = self.cleaned_data.get('password')
        user = authenticate(email = email, password = password)

        if (user is None) or (user is not None and user.is_active is False):
            raise forms.ValidationError('Login is incorrect.')

        return user

但是,当我在我的站点上将调试设置为 false 时,只要登录不正确,我就会收到 500 错误,而不是显示 ValidationError 的表单。我这样称呼它:

if form.is_valid():
    user = form.Login()

那是在视图方法中。我在这里想念什么?如何正确调用此表单错误,使其在调试设置为 false 时不会出现 500?

【问题讨论】:

标签: django forms


【解决方案1】:

这样打电话

def clean(self):
    password = self.cleaned_data.get('password')
    email = self.cleaned_data.get('email')   
    user = authenticate(email = email, password = password)
    if (user is None) or (user is not None and user.is_active is False):
        raise forms.ValidationError('Login is incorrect.')
    return self.cleaned_data

def login(self,request):
    user = authenticate(email = self.cleaned_data.get('email'),password = self.cleaned_data.get('password'))
    user = authenticate(username=username, password=password)
    return user

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 2020-02-04
    • 2022-01-07
    • 2011-01-03
    • 2017-09-30
    • 1970-01-01
    相关资源
    最近更新 更多