国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

django 通過ajax完成郵箱用戶注冊、激活賬號的方法

2020-02-22 23:40:37
字體:
來源:轉載
供稿:網友

一、圖片驗證碼

django-simple-captcha配置

1.在pycharm中,File====》Settings====》Project:項目名====》Project Interpreter====》+====》搜django-simple-captcha  選擇0.55以上版本,然后點install package 按鈕進行安裝

2.項目名/urls.py中添加代碼:

from django.urls import path,include......from users.views import IndexView......urlpatterns = [  ......    #配置驗證碼  path('captcha/',include('captcha.urls')),  #首頁url  path('', IndexView.as_view(), name='index'),  ......]

3.settings.py中添加一個注冊信息

INSTALLED_APPS = [    ......    'captcha']

4.打開終端Terminal執行更新數據庫命令:

python manage.py makemigrationspython manage.py migrate

5.在users目錄下新建form.py文件:

from django import formsfrom captcha.fields import CaptchaField......class RegisterForm(forms.Form):  """注冊信息的驗證"""  ......  captcha=CaptchaField(error_messages={'invalid':'驗證碼錯誤'})  ......

6.在users/views.py中添加代碼:

from users.form import RegisterFormclass IndexView(View):  """首頁"""  def get(self,request):    register_form=RegisterForm()    return render(request,'index.html',{'register_form':register_form})

7.在前端首頁index.html中顯示驗證碼、輸入框

html

<div class="modal fade" id="register" tabindex="-1" role="dialog">  ......<!--模態框中關于注冊的內容start-->   <div class="modal-body">    ......            <P><div style="display: inline-block;width:100px;text-align: center"><b >驗證碼:</b></div><!--驗證碼start-->      <div class="cap">{{ register_form.captcha }}</div><!--驗證碼end--></P>      {% csrf_token %}    </form>     <p><div style="margin-left: 100px;background-color: orangered;width:150px;text-align: center"><b></b></div></p>   </div><!--模態框中關于注冊的內容end-->  ......

css

<style>  .cap{    display: inline-block;    width: 280px;    height: 36px;  }  .cap img{    float: right;  }</style>

js 跟刷新驗證碼相關(需要先引入jQuery)

$(function(){    $('.captcha').css({    'cursor': 'pointer'  });  /*# ajax 刷新*/    $('.captcha').click(function(){      console.log('click');      $.getJSON("/captcha/refresh/",function(result){        $('.captcha').attr('src', result['image_url']);        $('#id_captcha_0').val(result['key'])      });    });  })

二、ajax郵箱注冊

 1.在前端跟注冊綁定的模態框代碼寫成:

html

<div class="modal fade" id="register" tabindex="-1" role="dialog"> ......      <div class="modal-body">    <form id="registerform" action="#" method="post">      <p>        <div class="re-input"><b>用戶名:</b></div>        <input type="text" name="user" placeholder="用戶名">        <div class="msg"><b id="user-msg"></b></div>      </p>      <p>        <div class="re-input"><b>郵箱:</b></div>        <input type="text" name="email" placeholder="郵箱">        <div class="msg"><b id="email-msg">2222</b></div>      </p>      <p>        <div class="re-input"><b >密碼:</b></div>        <input type="password" name="pwd" placeholder="密碼(不少于6位)">        <div class="msg"><b id="pwd-msg">222</b></div>      </p>      <p>        <div class="re-input"><b >確認密碼:</b></div>        <input type="password" name="pwd2" placeholder="確認密碼">        <div class="msg"><b id="pwd-msg2">22</b></div>      </p>      <P><div class="re-input"><b >驗證碼:</b></div>        <div class="cap">{{ register_form.captcha }}</div>        <div class="msg"><b id="captcha-msg">2</b></div>      </P>      {% csrf_token %}    </form>     <p><div style="margin-left: 100px;color: white;background-color: green;width:180px;text-align: center"><b id="active-msg"></b></div></p>   ......    <button type="button" class="btn btn-primary" id="registerbtn">確認注冊</button>   ......            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 会泽县| 民权县| 三原县| 东乡| 临湘市| 青田县| 民丰县| 福鼎市| 晋中市| 鄯善县| 犍为县| 邵东县| 逊克县| 礼泉县| 八宿县| 郎溪县| 麦盖提县| 临澧县| 磐安县| 桦南县| 濮阳市| 敖汉旗| 平昌县| 永康市| 兴仁县| 普洱| 上饶县| 南京市| 大港区| 安庆市| 稷山县| 荣成市| 开原市| 和政县| 顺平县| 行唐县| 开平市| 高平市| 吉首市| 新密市| 民权县|