前端HTML <!DOCTYPE html> {% load static %} {% get_static_prefix as getstatic %} <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta http-equiv="x-ua ...
前端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 }} <span class="text-danger"></span> </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 }} <span class="text-danger field-validation-error"><span id="DisplayName-error"></span></span> </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 }} <span class="text-danger field-validation-error"></span> </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 }} <span class="text-danger field-validation-error"><span id="mobile-error"></span></span> </div> </div> <!--註冊按鈕--> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <span class="col-sm-12 ajax-error"></span> <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"> <span class="asterisk">*</span> “註冊” 按鈕,即表示您同意並願意遵守 <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>View Code
上面的代碼只是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 "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主要用於將文件內容讀入記憶體,通過一系列非同步介面,可以在主線程中訪問本地文件。
使用FileReader對象,web應用程式可以非同步的讀取存儲在用戶電腦上的文件(或者原始數據緩衝)內容,可以使用File對象或者Blob對象來指定所要處理的文件或數據。
更多關於FileReader()
設計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) // 那麼視圖將再次創建一個表單實例並使用請求中的數據填充它:form = NameForm(request.POST)
。這叫做”綁定數據至表單“(它現在是一個綁定的表單)。 if register_form.is_valid(): //is_valid()
方法;如果它不為True
,我們將帶著這個表單返回到模板。這時表單不再為空(未綁定),所以HTML 表單將用之前提交的數據填充,然後可以根據要求編輯並改正它。 email = request.POST.get("email") //is_valid()
為True
,我們將能夠在cleaned_data
屬性中找到所有合法的表單數據。在發送HTTP 重定向給瀏覽器告訴它下一步的去向之前,我們可以用這個數據來更新資料庫或者做其它處理。 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("")