django captcha和郵箱驗證機制

驗證碼插件--django captcha和郵箱驗證機制

對於web開發在用戶註冊登陸的環節的驗證碼是在開發中必不可少的一個環節,這裏介紹一下我在開發中常用到的一個帶三方的驗證碼插件--
captchahtml

1、安裝

在GitHub上能夠直接下載:前端

也能夠使用pip進行安裝:web

2、導入

1.settings.py文件數據庫

這裏須要在INSTALLED_APPS中將其註冊進去:django

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'App01',
    'crispy_forms',
    'captcha',
]

2.form.py文件後端

由於是作用戶註冊使用,這裏定義一個註冊使用的form表單session

from django import forms
from captcha.fields import CaptchaField

class RegisterForm(forms.Form):
    username = forms.CharField(required=True)
    password = forms.CharField(required=True,min_length=5)
    email = forms.EmailField(required=True)
    captcha = CaptchaField()  #直接引用

3.view.py文件post

from . import forms
class User_reg(View):
def get(self,request):
    register_form = forms.RegisterForm
    return render(request,'reg.html',{"register_form":register_form})  #form表單發送到前端進行渲染
def post(self,request):
    pass

4.models.py文件ui

class EmailVerifyRecord(models.Model):
    code = models.CharField(max_length=20,verbose_name="驗證碼")
    email = models.EmailField(max_length=50,verbose_name="郵箱")
    send_type = models.CharField(verbose_name="驗證碼類型",choices=(("register","註冊"),("forget","找回密碼")),max_length=50)
    send_time = models.DateTimeField(verbose_name="發送時間",default=datetime.now)

4.reg.html頁面加密

5.前端渲染的效果:

下面是頁面的源碼

後端數據庫生成的id值

3、用戶註冊驗證

1.views.py文件

from django.contrib.auth.hashers import make_password  #用來將明文密碼進行加密
class User_reg(View):
    def get(self,request):
        # register_form = forms.RegisterForm
        return render(request,'reg.html',{})
    def post(self,request):
        register_form = forms.RegisterForm(request.POST)
        if register_form.is_valid():
            user_name = request.POST.get('username','')
            user_email = request.POST.get("email",'')
            pass_word = request.POST.get("password",'')
            user_profile = models.Userinfo()
            user_profile.username = user_name
            user_profile.email = user_email
            user_profile.password=make_password(pass_word)
            user_profile.save()
            send_register_email(user_email,"register")
            return render(request,'login.html')
        else:
            return render(request,"reg.html",{"register_form":register_form})

激活

1.urls.py文件

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url('^$',TemplateView.as_view(template_name="index.html"),name='index'),
    url(r'^login/$',views.User_login.as_view(),name='login' ),
    url(r'^reg/$',views.User_reg.as_view(),name='reg' ),
    url(r'^active/(?P<acctive_code>.*)/$',views.AciveUserView.as_view(),name='user_active'), #用戶進行郵箱激活的url
]

2.views.py文件

class AciveUserView(View):
    def get(self,request,active_code):
        all_records = models.EmailVerifyRecord.objects.filter(code=active_code)
        if all_records:
            for record in all_records:
                email = record.email
                user = models.Userinfo.get(email=email)
                user.is_active = True  #這個用戶表中的一個字段,用於驗證用戶是否進行郵政驗證激活過
                user.save()
        return render(request,'login.html')
相關文章
相關標籤/搜索