python - 如何在 Django 自定义身份验证后端中出现错误消息

标签 python django templates authentication backend

我已经为我的 django 项目编写了一个新的身份验证后端,但是当用户名或密码不正确时,我无法在输出中显示错误消息。

这是我的身份验证功能代码:

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

当我的用户名和密码不正确时,我不知道该怎么编码。

我的登录模板是这样的:

{% 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 %}

请尽快帮助我;-)

THNX 穆罕默德

最佳答案

您需要在身份验证后端保存错误:

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

然后像在 django.contrib.auth.forms 中那样编写你的 AuthenticationForm 并修改 clean 方法:

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

关于python - 如何在 Django 自定义身份验证后端中出现错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14002173/

相关文章:

templates - 模板类的 CUDA 链接器错误

c++ - templated operator string() 在临时对象时不会编译

python - 为什么一百万个随机数的平均值不在范围的中间?

python - 使用 urllib.urlretrieve() 保存图像

python - 识别django中数据库对象是什么类型

Django 网址设计

c# - 允许用户内联编写脚本,.net 或 java 有哪些内联脚本引擎?

python - 当在 python 中使用任何 Web 请求库时,响应在我的公司 PC 上超时,但在我的个人 PC 上却没有?

python - pandas 数据框用作神经网络的输入?

python - Python 中的 Try/except 语法有两个相同的异常