Add captcha field for django admin's login page.
Project description
django-admin-safe-login
Add captcha field for django admin's login page.
Install
pip install django-admin-safe-login
Usage
pro/settings.py
INSTALLED_APPS = [
...
'django_static_jquery3',
'django_admin_safe_login',
'captcha',
...
]
CAPTCHA_IMAGE_SIZE = (100, 30)
ENABLE_LOGIN_CAPTCHA = True
Note:
- Insert django_static_jquery3, django_admin_safe_login and captcha into INSTALLED_APPS.
- Configuration item CAPTCHA_IMAGE_SIZE is required, and must set to (100, 30) so that it will not break the display style. If you want other size image, you have to rewrite some css code.
- Configuration item ENABLE_LOGIN_CAPTCHA is optional, it's default to True value. You can use it for disable captcha for a short time. If you want remove the captcha functional, just remove the app django_admin_safe_login.
pro/urls.py
from django.urls import path
from django.urls import include
urlpatterns = [
...
path('captcha/', include("captcha.urls")),
...
]
Note:
- Include captcha.urls is required so that the captcha image can be displayed.
When things go down!
We have override some part of admin/login.html. But the admin/login.html content may change in future releases. So you should known what part is overrided.
{% extends "admin/login.html" %}
{% load i18n static %}
{% block extrastyle %}
{{ block.super }}
<!-- resource files included below is ours -->
<link rel="stylesheet" type="text/css" href="{% static "django-admin-safe-login/css/django-admin-safe-login.css" %}" />
<script src="{% static "jquery3/jquery.js" %}"></script>
<script src="{% static "django-admin-safe-login/js/django-admin-safe-login.js" %}"></script>
<!-- resource files included above is ours -->
{% endblock %}
{% block content %}
.....
<form action="{{ app_path }}" method="post" id="login-form">{% csrf_token %}
......
<div class="form-row">
{{ form.password.errors }}
{{ form.password.label_tag }} {{ form.password }}
....
</div>
<!-- form-row below is ours -->
<div class="form-row">
<label for="id_captcha" class="required">{% trans "Captcha" %}</label>
<div class="captcha-input">
{{ form.captcha }}
</div>
{% if not form.this_is_the_login_form.errors %}{{ form.captcha.errors }}{% endif %}
</div>
<!-- form-row above is ours -->
......
</form>
......
{% endblock %}
Steps:
- Our login.html is extends from "admin/login.html"
- Override block extrastyle, append our css and js.
- Override block content, all content of this block must copy from "admin/login.html", and add our form-row after form-row of password.
Releases
v0.1.0 2020/03/06
- First release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file django-admin-safe-login-0.1.0.tar.gz
.
File metadata
- Download URL: django-admin-safe-login-0.1.0.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a83858d345cf64881a349596096421196e41778a8624c7d61706b7d2732432b8 |
|
MD5 | 4b4b49022aac60a4b0c20377d73381f9 |
|
BLAKE2b-256 | 0652fef2c0baccedc3b496f0ef48b324a23609bd7564515d56f2387b65b7b027 |