Created
October 21, 2023 02:21
-
-
Save sunfkny/5effd8d28b2d55797f5e3efbeb3d8e7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "admin/base_site.html" %} | |
{% load i18n static %} | |
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" href="{% static "admin/css/login.css" %}"> | |
<style> | |
#id_token { | |
padding: 8px; | |
width: 100%; | |
box-sizing: border-box; | |
} | |
</style> | |
{{ form.media }} | |
{% endblock %} | |
{% block bodyclass %}{{ block.super }} login{% endblock %} | |
{% block usertools %}{% endblock %} | |
{% block nav-global %}{% endblock %} | |
{% block nav-sidebar %}{% endblock %} | |
{% block content_title %}{% endblock %} | |
{% block breadcrumbs %}{% endblock %} | |
{% block content %} | |
{% if form.errors and not form.non_field_errors %} | |
<p class="errornote"> | |
{% if form.errors.items|length == 1 %}{% translate "Please correct the error below." %}{% else %}{% translate "Please correct the errors below." %}{% endif %} | |
</p> | |
{% endif %} | |
{% if form.non_field_errors %} | |
{% for error in form.non_field_errors %} | |
<p class="errornote"> | |
{{ error }} | |
</p> | |
{% endfor %} | |
{% endif %} | |
<div id="content-main"> | |
{% if user.is_authenticated %} | |
<p class="errornote"> | |
{% blocktranslate trimmed %} | |
You are authenticated as {{ username }}, but are not authorized to | |
access this page. Would you like to login to a different account? | |
{% endblocktranslate %} | |
</p> | |
{% endif %} | |
<form action="{{ app_path }}" method="post" id="login-form">{% csrf_token %} | |
<div class="form-row"> | |
{{ form.username.errors }} | |
{{ form.username.label_tag }} {{ form.username }} | |
</div> | |
<div class="form-row"> | |
{{ form.password.errors }} | |
{{ form.password.label_tag }} {{ form.password }} | |
<input type="hidden" name="next" value="{{ next }}"> | |
</div> | |
<div class="form-row"> | |
{{ form.token.errors }} | |
{{ form.token.label_tag }} {{ form.token }} | |
</div> | |
{% url 'admin_password_reset' as password_reset_url %} | |
{% if password_reset_url %} | |
<div class="password-reset-link"> | |
<a href="{{ password_reset_url }}">{% translate 'Forgotten your password or username?' %}</a> | |
</div> | |
{% endif %} | |
<div class="submit-row"> | |
<input type="submit" value="{% translate 'Log in' %}"> | |
</div> | |
</form> | |
</div> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.admin.forms import AdminAuthenticationForm | |
from django.core.exceptions import ValidationError as DjangoValidationError | |
from django import forms | |
class DynamicTokenLoginForm(AdminAuthenticationForm): | |
token = forms.CharField( | |
label="动态令牌值", | |
strip=True, | |
required=True, | |
help_text="请输入6位动态令牌值", | |
widget=forms.TextInput(), | |
) | |
error_messages = { | |
**AdminAuthenticationForm.error_messages, | |
"invalid_dynamic_token": "%(msg)s", | |
} | |
def clean(self): | |
token = self.cleaned_data.get("token") | |
if not token: | |
raise DjangoValidationError("请输入动态令牌值") | |
if token != "123456": | |
raise DjangoValidationError( | |
self.error_messages["invalid_dynamic_token"], | |
code="invalid_dynamic_token", | |
params={"msg": "动态令牌值错误"}, | |
) | |
super().clean() | |
return self.cleaned_data | |
admin.site.login_template = "admin/login_with_token.html" | |
admin.site.login_form = DynamicTokenLoginForm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment