xml地图|网站地图|网站标签 [设为首页] [加入收藏]

基于Ajax和forms组件实现注册功能的实例代码,利

来源:http://www.ccidsi.com 作者:呼叫中心培训课程 人气:111 发布时间:2019-06-05
摘要:前端HTML 听别人说Ajax和forms组件达成登记效能的实例代码,ajaxforms 前端HTML !DOCTYPE html{% load static %}{% get_static_prefix as getstatic %}html lang="zh-CN"head meta charset="UTF-8" meta http-equiv="x-ua-compatible"

前端HTML

听别人说Ajax和forms组件达成登记效能的实例代码,ajaxforms

前端HTML

<!DOCTYPE html>
{% load static %}
{% get_static_prefix as getstatic %}
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="x-ua-compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" href="https://www.jd.com/favicon.ico">
  <link href="{{ getstatic }}plugins/bootstrap-3.3.7/css/bootstrap.min.css" rel="stylesheet">
  <title>注册页面</title>
  <style>
    .title-top {
      padding: 0;
      font-size: 21px;
      margin-top: 40px;
    }
    .form-padding {
      padding-right: 65px;
    }
    .has-feedback .country-code {
      width: 53px;
      text-align: right;
      padding-right: 5px;
    }
    .mobile-control-wrap {
      display: flex;
    }
    .mobile-num {
      margin-left: 10px;
    }
    .form-horizontal .form-group {
      margin-right: -15px;
      margin-left: -15px;
    }
    .body-box {
      margin-top: 70px;
      height: 567px;
      width: 68%;
    }
    .cnblogs-btn-blue {
      color: white;
      padding: 6px 14px;
      letter-spacing: 1em;
      padding-left: 2em;
      background-color: RGB(70, 136, 214);
      text-align: center;
    }
    .cnblogs-btn-blue :hover {
      color: white;
    }
    .navbar {
      background-color: RGB(40, 62, 92);
    }
    .side-img-box {
      height: 300px;
      position: relative;
      padding-left: 0;
      margin-top: 168px;
    }
    .side-img-box img {
      position: relative;
      width: 100%;
      top: 50%;
      transform: translateY(-50%);
      border-radius: 8px;
    }
    .text-danger {
      float: right;
    }
    .img-thumbnail {
      width: 100px;
      height: 100px;
      margin-bottom: 20px;
      margin-top: -10px;
    }
    #avatr_img {
      margin-left: 237px;
    }
  </style>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
  <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <img src="{{ getstatic }}img/TIM截图20180205110238.png">
    </div>
    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">首页</a></li>
        <li><a href="#">登录</a></li>
        <li><a href="#">注册</a></li>
        <li><a href="#">帮助</a></li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
<div class="container body-box" style="height: 567px;">
  <div class="center-block body-content">
    <!--头部提示-->
    <div class="hidden-xs title-top col-sm-12">
      注册新用户
      <hr class="head-hr">
    </div>
    <div class="form-wrap">
      <form class="form-horizontal col-sm-8 form-padding" role="form" id="registerForm" method="post"
         novalidate="novalidate">
        <!--每一行-->
        <div id="avatr_img" class="form-group">
          <label for="avatar">
            <img id="avatar_img" class="img-thumbnail" src="{{ getstatic }}img/registersideimg.png" alt="用户头像">
            头像
          </label>  
          <input type="file" style="display: none;" id="avatar">
        </div>
        <div class="form-group">
          <div class="col-sm-2 control-label">
            <label class="w4-2" for="Email">邮       箱</label>
          </div>
          <div class="col-sm-10 has-feedback">
            {{ register_form.email }}

          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-2 control-label">
            <label class="w4-4" for="DisplayName">昵称</label>
          </div>
          <div class="col-sm-10 has-feedback">
            {{ register_form.nik_name }}

          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-2 control-label">
            <label class="w4-2" for="Password">密       码</label>
          </div>
          <div class="col-sm-10 has-feedback">
            {{ register_form.password }}

          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-2 control-label">
            <label class="w4-4" for="ConfirmPassword">确认密码</label>
          </div>
          <div class="col-sm-10 has-feedback">
            {{ register_form.repeta_pwd }}

          </div>
        </div>
        <!--注册按钮-->
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">

            <input id="submitBtn" onclick="regesite()" type="button"
                class="btn ladda-button center-block cnblogs-btn-blue" value="登录">
          </div>
        </div>
        <!--协议提示-->
        <div class="col-sm-offset-2 register-sign">
          * “注册” 按钮,即表示您同意并愿意遵守 <a class="look-agreeon" target="_blank"
                                      href="//passport.cnblogs.com/agreement.html">用户协议</a>。
        </div>
        <!--hidden-->
        {% csrf_token %}
      </form>
      <div class="hidden-xs col-sm-4 side-img-box">
        <div class="side-line">
          <div></div>
        </div>
        <img src="{{ getstatic }}img/registersideimg.png">
      </div>
    </div>
  </div>
</div>
<script src="{{ getstatic }}js/jquery-3.2.1.min.js"></script>
<script src="{{ getstatic }}plugins/bootstrap-3.3.7/js/bootstrap.min.js"></script>
</body>
</html>

地方的代码只是HTML ,而JavaScript在上边实例中

<script>
   $("#avatar").change(function () {
    var chooice_file = $(this)[0].files[0];
    var reader = new FileReader();// 注意这里,预览本地文件
    reader.readAsDataURL(chooice_file);
    reader.onload=function () {
      $("#avatar_img").attr("src",this.result)
    }
  })
  function regesite() {
    var formdata = new FormData(); //z注意这里 涉及文件上传
    formdata.append("email",$("#id_email").val());
    formdata.append("nik_name",$("#id_nik_name").val());
    formdata.append("password",$("#id_password").val());
    formdata.append("repeta_pwd",$("#id_repeta_pwd").val());
    formdata.append("valid_img",$("#avatar")[0].files[0]);
    formdata.append("csrfmiddlewaretoken",$("[name='csrfmiddlewaretoken']").val());
    $.ajax({
      url: "{% url "regeste" %}",
      type: "POST",
      data:formdata,
      contentType:false,
      processData:false,
      success: function (data) {
        response_data = JSON.parse(data)
        if(response_data.user){
          location.href="{% url " rel="external nofollow" login" %}"
        }else {
          // 清空上次错误信息
          $("form span").html("")
          $(".has-feedback").removeClass("has-error")
          $.each(response_data.error_msg,function (fiel, error_info) {
            // 显示错误信息
            $("#id_" fiel).parent().addClass("has-error");
            $("#id_" fiel).next().html(error_info[0]);//显示全局错误 // 注意这里
            if(fiel=="__all__"){
              $("#id_repeta_pwd").next().html(error_info[0]).css("color","red")
            }
          })
        }
      }
    })
  }
</script>

FileReader

FileReader主要用以将文件内容读入内部存款和储蓄器,通过1多级异步接口,能够在主线程中走访当半夏件。

应用FileReader对象,web应用程序能够异步的读取存款和储蓄在用户Computer上的公文(或然原有数据缓冲)内容,能够运用File对象或许Blob对象来内定所要管理的文书或数额。

设计RegisterForm组件

from django import forms
from . import models
from django.forms import widgets
from django.core.exceptions import NON_FIELD_ERRORS, ValidationError
class RegisterForm(forms.Form):
  nik_name = forms.CharField(max_length=32,
                widget=widgets.TextInput(attrs={"class":"form-control"}),
                error_messages={"required":"用户不能为空"})
  email = forms.EmailField(widget=widgets.EmailInput(attrs={"class":"form-control"}),
               error_messages={"required":"邮箱不能为空", "invalid":"邮箱格式错误"})
  password = forms.CharField(min_length=5, widget=widgets.PasswordInput(attrs={"class":"form-control"}),
                error_messages={"required": "密码不能为空",
                        "min_length":"最小长度5位",
                        }
                )
  repeta_pwd = forms.CharField(widget=widgets.PasswordInput(attrs={"class":"form-control"}),
                 error_messages={"required": "密码不能为空"})
  def clean_nik_name(self):
    name = self.cleaned_data.get("nik_name")
    users = models.UserInfo.objects.filter(nik_name=name)
    if not users:
      return name
    else:
      raise ValidationError("用户已存在")
  def clean(self): //全局钩子
    pwd = self.cleaned_data.get("password")
    repeta_pwd = self.cleaned_data.get("repeta_pwd")
    if pwd and repeta_pwd:
      if pwd == repeta_pwd:
        return self.cleaned_data
      else:
        raise ValidationError("两次密码不一致")
    else:
      return self.cleaned_data

皇家娱乐平台,服务器

def post(self,request):
    reg_response = {"user": None,"error_msg":""}
    register_form = RegisterForm(request.POST)
    if register_form.is_valid():
      email = request.POST.get("email")
      nik_name = request.POST.get("nik_name")
      password = request.POST.get("password")
      repeta_pwd = request.POST.get("repeta_pwd")
      valid = request.FILES.get("valid_img") #注意这里使用的request.FILES.get("")
      if not valid:
        valid = "avatardir/TIM图片20171209211626.gif"
      check_user = models.UserInfo.objects.create_user(username=nik_name,nik_name=nik_name, email=email,
                                password=password,
                                avatar=valid
                              ) # 注意这里保存导数据库中的是图片路径,并不是图片
      reg_response = {"user": "user", "error_msg": ""}
      return HttpResponse(json.dumps(reg_response))
    else:
      reg_response["error_msg"]=register_form.errors
      return HttpResponse(json.dumps(reg_response))

Model中 Userifor 的img

avatar = models.FileField(upload_to='avatar/', default="/avatar/default.gif", verbose_name="主键")

注意:

  upload_to :默许把公文夹创造至根目录下,如成立至钦定地方,供给在setting中布局media。

     如:希望用户上传的文书单独存款和储蓄,在setting中布署,

MEDIA_ROOT= os.path.jion(BASE_DIR,"blog","media")
//此时,用户上传文件时,先创建并将文件保存至“putImg”文件夹中,“putImg”,然后将“putImg”放至对应的路径中。
# media 配置
url(r"^media/(?P<path>.*)",sever,{"document_root":setting.MEDIA_ROOT})

布置好地点的url 后,用户能够访media 文件夹下边包车型大巴装有文件

   注意:    

 server ,是从django.views.static import server导入
     from . import settings //url中的使用

总结

上述所述是作者给大家介绍的依照Ajax和forms组件完毕挂号功用,希望对大家有所辅助,假设我们有其它疑问请给自个儿留言,小编会及时还原我们的。在此也特别谢谢大家对帮客之家网址的支撑!

前端HTML !DOCTYPE html{% load static %}{% get_static_prefix as getstatic %}html lang="zh-CN"head meta charse...

FileReader 对象允许Web应用程序异步读取存款和储蓄在用户Computer上的文件(或原始数据缓冲区)的内容,使用 File或 Blob对象钦定要读取的文书或数量。

<!DOCTYPE html>
{% load static %}
{% get_static_prefix as getstatic %}
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="x-ua-compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" href="https://www.jd.com/favicon.ico">
  <link href="{{ getstatic }}plugins/bootstrap-3.3.7/css/bootstrap.min.css" rel="stylesheet">
  <title>注册页面</title>
  <style>
    .title-top {
      padding: 0;
      font-size: 21px;
      margin-top: 40px;
    }
    .form-padding {
      padding-right: 65px;
    }
    .has-feedback .country-code {
      width: 53px;
      text-align: right;
      padding-right: 5px;
    }
    .mobile-control-wrap {
      display: flex;
    }
    .mobile-num {
      margin-left: 10px;
    }
    .form-horizontal .form-group {
      margin-right: -15px;
      margin-left: -15px;
    }
    .body-box {
      margin-top: 70px;
      height: 567px;
      width: 68%;
    }
    .cnblogs-btn-blue {
      color: white;
      padding: 6px 14px;
      letter-spacing: 1em;
      padding-left: 2em;
      background-color: RGB(70, 136, 214);
      text-align: center;
    }
    .cnblogs-btn-blue :hover {
      color: white;
    }
    .navbar {
      background-color: RGB(40, 62, 92);
    }
    .side-img-box {
      height: 300px;
      position: relative;
      padding-left: 0;
      margin-top: 168px;
    }
    .side-img-box img {
      position: relative;
      width: 100%;
      top: 50%;
      transform: translateY(-50%);
      border-radius: 8px;
    }
    .text-danger {
      float: right;
    }
    .img-thumbnail {
      width: 100px;
      height: 100px;
      margin-bottom: 20px;
      margin-top: -10px;
    }
    #avatr_img {
      margin-left: 237px;
    }
  </style>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
  <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <img src="{{ getstatic }}img/TIM截图20180205110238.png">
    </div>
    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">首页</a></li>
        <li><a href="#">登录</a></li>
        <li><a href="#">注册</a></li>
        <li><a href="#">帮助</a></li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
<div class="container body-box" style="height: 567px;">
  <div class="center-block body-content">
    <!--头部提示-->
    <div class="hidden-xs title-top col-sm-12">
      注册新用户
      <hr class="head-hr">
    </div>
    <div class="form-wrap">
      <form class="form-horizontal col-sm-8 form-padding" role="form" id="registerForm" method="post"
         novalidate="novalidate">
        <!--每一行-->
        <div id="avatr_img" class="form-group">
          <label for="avatar">
            <img id="avatar_img" class="img-thumbnail" src="{{ getstatic }}img/registersideimg.png" alt="用户头像">
            头像
          </label>  
          <input type="file" style="display: none;" id="avatar">
        </div>
        <div class="form-group">
          <div class="col-sm-2 control-label">
            <label class="w4-2" for="Email">邮       箱</label>
          </div>
          <div class="col-sm-10 has-feedback">
            {{ register_form.email }}

          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-2 control-label">
            <label class="w4-4" for="DisplayName">昵称</label>
          </div>
          <div class="col-sm-10 has-feedback">
            {{ register_form.nik_name }}

          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-2 control-label">
            <label class="w4-2" for="Password">密       码</label>
          </div>
          <div class="col-sm-10 has-feedback">
            {{ register_form.password }}

          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-2 control-label">
            <label class="w4-4" for="ConfirmPassword">确认密码</label>
          </div>
          <div class="col-sm-10 has-feedback">
            {{ register_form.repeta_pwd }}

          </div>
        </div>
        <!--注册按钮-->
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">

            <input id="submitBtn" onclick="regesite()" type="button"
                class="btn ladda-button center-block cnblogs-btn-blue" value="登录">
          </div>
        </div>
        <!--协议提示-->
        <div class="col-sm-offset-2 register-sign">
          * “注册” 按钮,即表示您同意并愿意遵守 <a class="look-agreeon" target="_blank"
                                      href="//passport.cnblogs.com/agreement.html">用户协议</a>。
        </div>
        <!--hidden-->
        {% csrf_token %}
      </form>
      <div class="hidden-xs col-sm-4 side-img-box">
        <div class="side-line">
          <div></div>
        </div>
        <img src="{{ getstatic }}img/registersideimg.png">
      </div>
    </div>
  </div>
</div>
<script src="{{ getstatic }}js/jquery-3.2.1.min.js"></script>
<script src="{{ getstatic }}plugins/bootstrap-3.3.7/js/bootstrap.min.js"></script>
</body>
</html>

一、FileReader接口的章程

上边的代码只是HTML ,而JavaScript在底下实例中

FileReader接口有四个主意,在那之中三个用来读取文件,另1个用来行车制动器踏板读取。无论读取成功或失败,方法并不会回来读取结果,那壹结实存款和储蓄在result属性中。

<script>
   $("#avatar").change(function () {
    var chooice_file = $(this)[0].files[0];
    var reader = new FileReader();// 注意这里,预览本地文件
    reader.readAsDataURL(chooice_file);
    reader.onload=function () {
      $("#avatar_img").attr("src",this.result)
    }
  })
  function regesite() {
    var formdata = new FormData(); //z注意这里 涉及文件上传
    formdata.append("email",$("#id_email").val());
    formdata.append("nik_name",$("#id_nik_name").val());
    formdata.append("password",$("#id_password").val());
    formdata.append("repeta_pwd",$("#id_repeta_pwd").val());
    formdata.append("valid_img",$("#avatar")[0].files[0]);
    formdata.append("csrfmiddlewaretoken",$("[name='csrfmiddlewaretoken']").val());
    $.ajax({
      url: "{% url "regeste" %}",
      type: "POST",
      data:formdata,
      contentType:false,
      processData:false,
      success: function (data) {
        response_data = JSON.parse(data)
        if(response_data.user){
          location.href="{% url " rel="external nofollow" login" %}"
        }else {
          // 清空上次错误信息
          $("form span").html("")
          $(".has-feedback").removeClass("has-error")
          $.each(response_data.error_msg,function (fiel, error_info) {
            // 显示错误信息
            $("#id_" fiel).parent().addClass("has-error");
            $("#id_" fiel).next().html(error_info[0]);//显示全局错误 // 注意这里
            if(fiel=="__all__"){
              $("#id_repeta_pwd").next().html(error_info[0]).css("color","red")
            }
          })
        }
      }
    })
  }
</script>

皇家娱乐平台 1

FileReader

贰、File里德r接口事件

FileReader首要用来将文件内容读入内部存款和储蓄器,通过一名目繁多异步接口,能够在主线程中访问当守田件。

FileReader接口包含了1套完整的事件模型,用于捕获读取文件时的情况。

行使FileReader对象,web应用程序能够异步的读取存款和储蓄在用户Computer上的文本(也许原有数据缓冲)内容,能够选择File对象可能Blob对象来钦赐所要管理的文件或数量。

皇家娱乐平台 2

设计RegisterForm组件

重在介绍下:readAsDataU帕杰罗L

本文由68399皇家赌场发布于呼叫中心培训课程,转载请注明出处:基于Ajax和forms组件实现注册功能的实例代码,利

关键词: 68399皇家赌场

上一篇:皇家娱乐平台登陆可拖拽弹出层

下一篇:没有了

最火资讯