diff --git a/groups/forms.py b/groups/forms.py index 0876b1aa..f941e967 100644 --- a/groups/forms.py +++ b/groups/forms.py @@ -1,6 +1,7 @@ from django import forms - +from captcha.fields import CaptchaField class GroupsForm(forms.Form): username = forms.CharField(label="用户名", max_length=128, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': "Username",'autofocus': ''})) password = forms.CharField(label="密码", max_length=256, widget=forms.PasswordInput(attrs={'class': 'form-control','placeholder': "Password"})) + captcha = CaptchaField(label='验证码', error_messages={"invalid":"验证码错误"}) diff --git a/groups/templates/groups/login.html b/groups/templates/groups/login.html index 1ca7f9ba..cdd957b0 100644 --- a/groups/templates/groups/login.html +++ b/groups/templates/groups/login.html @@ -15,8 +15,10 @@
+
+ {{ groups_form.captcha.label_tag }} + {{ groups_form.captcha }} +
@@ -42,4 +48,13 @@ - \ No newline at end of file + + \ No newline at end of file diff --git a/groups/urls.py b/groups/urls.py index cff6c2a0..cd2b54b4 100644 --- a/groups/urls.py +++ b/groups/urls.py @@ -1,4 +1,5 @@ from django.urls import path +from django.urls import include from . import views urlpatterns = [ @@ -6,5 +7,6 @@ urlpatterns = [ path('index/', views.index, name='index'), path('login/', views.login, name='login'), path('logout/', views.logout, name='logout'), - + path('captcha/', include('captcha.urls')), + path('refresh_captcha/', views.refresh_captcha), ] \ No newline at end of file diff --git a/groups/views.py b/groups/views.py index 3581d830..231af8db 100644 --- a/groups/views.py +++ b/groups/views.py @@ -45,3 +45,14 @@ def logout(request): return redirect('/groups/login') request.session.flush() return redirect('/groups/login') + +import json +from captcha.models import CaptchaStore +from captcha.helpers import captcha_image_url +def refresh_captcha(request): + hashkey = CaptchaStore.generate_key() + image_url = captcha_image_url(hashkey) + return HttpResponse(json.dumps({'key': hashkey, 'image_url': image_url}), content_type='application/json') + + + diff --git a/mysite/settings.py b/mysite/settings.py index 1a9a43bb..b1fb09f1 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -39,6 +39,7 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', 'safesite', 'groups', + 'captcha', ] MIDDLEWARE = [ diff --git a/safesite/migrations/0268_merge_20191009_0952.py b/safesite/migrations/0268_merge_20191009_0952.py new file mode 100644 index 00000000..45a06ae3 --- /dev/null +++ b/safesite/migrations/0268_merge_20191009_0952.py @@ -0,0 +1,15 @@ +# Generated by Django 2.1.4 on 2019-10-09 09:52 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('safesite', '0265_auto_20190917_1716'), + ('safesite', '0267_auto_20190917_1543'), + ('safesite', '0265_companyinfo'), + ] + + operations = [ + ]