pip install django-simple-captcha
INSTALLED_APPS中
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users.apps.UsersConfig',
'orgs.apps.OrgsConfig',
'operations.apps.OperationsConfig',
'courses.apps.CoursesConfig',
'xadmin',
'crispy_forms',
'captcha'
]
urlpatterns = [
# url(r'^admin/', admin.site.urls),
url(r'^xadmin/', xadmin.site.urls),
url(r'^media/(?P.*)$', serve, {"document_root": MEDIA_ROOT}),
url(r'^captcha/', include('captcha.urls')),
url(r'^$', index, name='index'),
url(r'^users/', include('users.urls', namespace='users')),
url(r'^orgs/', include('orgs.urls', namespace='orgs')),
url(r'^courses/', include('courses.urls', namespace='courses')),
url(r'^operations/', include('operations.urls', namespace='operations')),
]
迁移:python manage.py makemigrations
同步:python manage.py migrate
class UserRegisterForm(forms.Form):
email = forms.EmailField(required=True, error_messages={
'required': '邮箱不能为空'
})
password = forms.CharField(max_length=20, min_length=3, required=True, error_messages={
'max_length': '密码的最大长度是20位',
'min_length': '密码的最小长度是3位',
'required': '密码不能为空'
})
captcha = CaptchaField(error_messages={
'invalid': '验证码错误'
})
def user_register(request):
if request.method == 'GET':
user_register_form = UserRegisterForm()
return render(request, 'user/user_register.html', {
'user_register_form': user_register_form
})
else:
user_register_form = UserRegisterForm(request.POST)
if user_register_form.is_valid():
email = user_register_form.cleaned_data['email']
password = user_register_form.cleaned_data['password']
user = UserProfile.objects.filter(Q(email=email) | Q(username=email))
if user:
return render(request, 'user/user_register.html', {
'msg': '用户已存在'
})
else:
a = UserProfile()
a.email = email
a.username = email
a.set_password(password)
a.save()
send_email_verify.delay(email, 'register')
return HttpResponse('邮件已发送,请去激活')
else:
return render(request, 'user/user_register.html', {
'user_register_form': user_register_form
})
{{ user_register_form.captcha }}
{{ msg }}
{% for key,err in user_register_form.errors.item %}
{{ err }}
{% endfor %}