如何在Django的自定义身份验证后端的错误信息 [英] How to have an error message in django custom authentication backend

查看:282
本文介绍了如何在Django的自定义身份验证后端的错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个新的认证后端为我的Django项目,但我不能显示在输出错误消息时的用户名或密码不正确。

I have write a new authentication backend for my django project but i cannot show error messages in output when username or password is incorrect.

下面是我的身份验证功能code:

Here is my authenticate function code :

def authenticate(self,username=None,password=None ):
    try:
        authService = AuthenticationLocator().getAuthenticationHttpSoap11Endpoint()
        authRequest = authenticateRequest()
        authRequest._Username = username
        authRequest._Password = password
        authResult = authService.authenticate(authRequest)

        if authResult._return[0] == 'true':

            try:
                user = User.objects.filter(username=username)
                if len(user) > 0:
                    usr = user[0]
                else:
                    usr = self.addUser(username)

#               Correct Login
                return usr

            except User.DoesNotExist:
                return None

        elif authResult._return[0] == 'error':
#           Connection Error
            return None
        elif authResult._return[0] == 'false':
#           InCorrect User
            return None
    except :
        return None

def get_user(self, user_id):
    try:
        return User.objects.get(pk=user_id)
    except User.DoesNotExist:
        return None

我不知道我应该怎么code时,我有一个不正确的用户名和密码。

I don't know what should I code when I have an incorrect username and password.

和我的登录模板是这样的:

and my login template is like this :

{% block content %}
{% if error_message %}
<p class="errornote">{{ error_message }}</p>
{% endif %}
<div id="content-main">
    <form action="{{ app_path }}" method="post" id="login-form">
        {{ form.non_field_error }}
        {% csrf_token %}
        <div>
            {{ message }}
        </div>
        <div class="form-row">
            <div class="form_cell"><label for="id_username">{% trans 'Username:' %}</label></div>
            <div class="form_cell"><input type="text" name="username" id="id_username" /></div>
        </div>
        <div class="form-row">
            <div class="form_cell"><label for="id_password">{% trans 'Password:' %}</label></div>
            <div class="form_cell"><input type="password" name="password" id="id_password" /></div>
            <input type="hidden" name="this_is_the_login_form" value="1" />
        </div>
        <div class="submit-row">
            <label>&nbsp;</label><input type="submit" value="{% trans 'Log in' %}" />
        </div>
    </form>
<script type="text/javascript">
document.getElementById('id_username').focus()
</script>
</div>
{% endblock %}

请尽快帮我; - )

Please help me as soon as possible ;-)

日Thnx
穆罕默德

THNX Mohammad

推荐答案

您需要保存的错误在你的认证后端:

You need to save error in your authentication backend:

def authenticate(self,username=None,password=None,errors=[]):
    ... 
    errors.append('Connection error.')

和编写AuthenticationForm作为清洁的方法修改django.contrib.auth.forms:

And write your AuthenticationForm as in django.contrib.auth.forms with modification of clean method:

def clean(self):
    ...
    errors = []
    self.user_cache = authenticate(username=username, password=password, errors=errors)
    ...
    raise form.ValidationError(" ".join(errors))

这篇关于如何在Django的自定义身份验证后端的错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆