一. 图片验证码
- 使用插件 django-simple-captcha
- 开源地址: https://github.com/mbi/django-simple-captcha
- 官方文档: http://django-simple-captcha.readthedocs.io/en/latest/usage.html#installation
- 安装:
Install django-simple-captcha
或者
pip install django-simple-captcha==0.6.4
- 使用步骤
1 添加app
# settings.py
INSTALLED_APPS = [
...
'captcha',
]
2 建立form表单类
# forms.py
#一般将一些表单类写到同一个py文件里
from captcha.fields import CaptchaField
class RegisterForm(forms.Form):
# 邮箱
email = forms.EmailField(required=True, )
# 密码验证
password = forms.CharField(required=True)
#invalid 表示出错时候的显示信息.
captcha = CaptchaField(error_messages={'invalid': '验证码输入有误'})
3 生成验证码
3.1 在加载页面时获得验证码
# views.py
# 创建对象
register_form = RegisterForm()
return render(request, 'register.html', {'register_form': register_form})
3.2 前端使用
<label>验 证 码</label>
{{ register_form.captcha }}
此时网页渲染时会自动生成验证码表单
<img src="/captcha/image/2f3f82e5f7a054bf5caa93b9b0bb6cc308fb7011/" alt="captcha" class="captcha" /> <input id="id_captcha_0" name="captcha_0" type="hidden"
value="2f3f82e5f7a054bf5caa93b9b0bb6cc308fb7011" /> <input autocomplete="off" id="id_captcha_1" name="captcha_1" type="text" />
4 对用户输入的验证码进行验证
#views.py
register_form = RegisterForm(request.POST)
if register_form.is_valid():
pass
这个app功能比较强大,它会自动提取 用户提交的验证码并与数据库进行比对,不需要开发人员进行操作比对
二. 邮箱注册验证激活
1 发送
1.1 创建邮箱激活链接
# apps目录下的 utils 文件夹下 的send_email.py
import random
#生成一个随机的字符串
def get_random_str(count):
random_str = ''
chars = 'fjdsalkfjdsklafjlkewjflkdjfsaflaeiw9ru'
str_len = len(chars) - 1
for i in range(count):
randindex = random.randint(0, str_len)
randomchar = chars[randindex]
str += randomchar
return random_str
1.2 具体的发送函数(省略生成随机字符串的函数)
# 同上
from django.core.mail import send_mail # 这个是django自带的邮件发送方法
def send_register_email(email, send_type='register'):
email_record = EmailVerifyRecord()
random_str = get_random_str(10)
email_record.code = random_str
email_record.email = email
email_record.type = send_type
email_record.save()
if send_type == 'register':
email_title = 'click here'
email_body = '请点击下边的链接激活 http://b.com/active/{0}'.format(random_str)
send_status = send_mail(email_title, email_body, settings.EMAIL_FROM, [email], html_message=email_body)
return send_status
1.3 用户注册时发送
# views.py
user = UserProfile()
user.username = username
user.password = make_password(password)
user.email = username
# 是否激活
user.is_active = False # 默认是不激活的 后面可以加限制条件 只有激活后才能登陆
user.save()
status = send_register_email(username, 'register')
1.4 setting.py设定初始
# 配置邮箱发邮件的相关功能
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.163.com'
EMAIL_PORT = 25
#发送邮件的邮箱
EMAIL_HOST_USER = '1111@163.com'
#在邮箱中设置的客户端授权密码
EMAIL_HOST_PASSWORD = ''
#收件人看到的发件人
EMAIL_FROM = '1111@163.com'
1.5. 用户收到邮件
2用户激活
2.1. 配置url
# 使用了命名组
url(r'^active/(?P<active_code>\w*)/$', views.Active.as_view(), name='user_active'),
2.2 根据url中active_code 获取对应的EmailVerifyRecord类对象
(注: 在1.2中创建了EmailVerifyRecord对象, 在1.3中在user对象中存储了email)
# views.py
all_records = EmailVerifyRecord.objects.filter(code=active_code)
2.3 根据EmailVerifyRecord对象获得相应用户并修改is_active字段.
# views.py
if all_records:
for record in all_records:
email = record.email
user = UserProfile.objects.get(email=email)
user.is_active = True # 修改该字段为True 即为激活
user.save()
return render(request, 'login.html', {'msg': '激活成功, 现在可以登录'})
2.4 改进登录view(限制未激活用户的登录)
# views.py
if user.is_active:
login(request, user)
# 登录成功, 则返回主页
return redirect('/')
else:
# 否则返回提示
return render(request, 'login.html', {'msg': '需要先激活'})
网友评论