我正在为我的 Django 应用程序制作登录页面。目前,如果我输入错误的用户名或密码,则会显示以下错误消息:
.
有没有办法可以自定义此消息以摆脱“___all____”和要点?也许只是说“请输入正确的用户名和密码。请注意,这两个字段都可能区分大小写。”?
我的代码的简化版本如下:
视图.py:
from django.contrib.auth import authenticate,login,logout
Run Code Online (Sandbox Code Playgroud)
html:
<div class="form_container">
<form class="log_in_form" action="{% url 'inventory_management_app:login' %}" method="post">
<p>
{% csrf_token %}
{{ form.username }}
</p>
<br><br>
<p>
{{ form.password }}
<br>
{{ form.errors }}
</p>
<p>
<br><br>
<input id="search-button" class="btn btn-dark" type="submit" value="Login">
</form>
</p>
</div>
Run Code Online (Sandbox Code Playgroud)
表格.py:
from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.forms.widgets import PasswordInput, TextInput
class CustomAuthForm(AuthenticationForm):
username = forms.CharField(widget=TextInput(attrs={'class':'validate','placeholder': 'Email'}))
password = forms.CharField(widget=PasswordInput(attrs={'placeholder':'Password'}))
Run Code Online (Sandbox Code Playgroud)
网址.py
url(r'^login/$', auth_views.login, name='login', kwargs={"authentication_form":CustomAuthForm}),
Run Code Online (Sandbox Code Playgroud)
更改表单类中的 invalid_login 错误消息。
class CustomAuthForm(AuthenticationForm):
def __init__(self, *args, **kwargs):
self.error_messages['invalid_login'] = 'Custom error'
super().__init__(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2111 次 |
| 最近记录: |