zoukankan      html  css  js  c++  java
  • django实现密码加密的注册(数据对象插入)-结合forms表单实现表单验证

    forms表单

    #_*_coding:utf-8_*_
    from django import  forms
    class regis(forms.Form):
        username = forms.CharField(label=u'用户名',min_length=4,max_length=20,error_messages={'required':u'用户名不能为空哦','min_length':u'用户名长度不能小于4位哦','max_length':u'用户名长度不能大于20位哦'})
        password = forms.CharField(label=u'密码',min_length=6,max_length=256,widget=forms.PasswordInput,error_messages={'required':u'密码不能为空哦','min_length':u'密码长度不能小于6位哦'})
        cpassword = forms.CharField(label=u'确认密码',min_length=6, max_length=256, widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':u'与上面密码保持一致'}),error_messages={'required': u'密码不能为空哦', 'min_length': u'密码长度不能小于6位哦'})
    

    models模型

    #coding=utf-8
    from django.db import models
    class Userinfo(models.Model):
        username = models.CharField(unique=True,max_length=20)
        password = models.CharField(max_length=256)
        def __unicode__(self):
            return self.username
    

    views视图

    #conding=utf-8
    from django.shortcuts import render,redirect
    from django.http import HttpResponse,Http404
    from forms import *
    from models import Userinfo
    from hashlib import sha1
    def register(request):
        if request.method == 'POST':
            form = regis(request.POST)
            if form.is_valid():
                user = form.cleaned_data['username']
                pwd = form.cleaned_data['password']
                cpwd = form.cleaned_data['cpassword']
                # encryption
                sh1 = sha1()
                sh1.update(pwd.encode('utf-8'))
                pwdd = sh1.hexdigest()
                if pwd != cpwd:
                    return redirect('/')
                Userinfo.objects.get_or_create(username=user,password=pwdd)
                msg='user register success!'
                return render(request,'info.html',{'msg':msg})
            else:
                error_msg = form.errors
                return render(request, 'register.html', {'form': form, 'errors': error_msg})
        else:
            form = regis()
            return render(request, 'register.html', {'form': form})
    

    注意视图的次序:这里的逻辑很重要

     if request.method == 'POST':
            form = regis(request.POST)
            if form.is_valid():

    使用表单进行数据提交时,不需要指定form的action,自动提交到展示页面的视图,切记注意

    先判断是提交信息还是普通访问

    如果不是提交信息,那么就打开注册页面同时携带表单对象,如果是post提交,就获取forms表单regis提交上来的信息

    再判断表单内容是否符合格式要求,如果符合再判断两次输入的密码是否相等,如果相等则进行数据插入的操作,并且返回info.html页面告知用户注册成功

    如果表单内容不符合格式要求,则将forms表单中的非空,长度限制等信息返回到注册页面

    注册页面register.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Login</title>
        <style type="text/css">
            .ff{
            margin-top:300px;
            margin-left:500px;
            }
            #log{
            background-color:#ccc;
            margin:0px auto;
            }
        </style>
    </head>
    <body>
    <div id="log">
    <form  method="post" class="ff">
        {% csrf_token %}
        <table>
            <tr><td>{{form.username.label_tag}}</td><td>{{form.username}}</td></tr>
            <tr style="color:red"><td>{{errors.username}}</td></tr>
            <tr><td>{{form.password.label_tag}}</td><td>{{form.password}}</td></tr>
            <tr style="color:red"><td>{{errors.password}}</td></tr>
            <tr><td>{{form.cpassword.label_tag}}</td><td>{{form.cpassword}}</td></tr>
            <tr style="color:red"><td>{{errors.cpassword}}</td></tr>
            <tr><td><input type="reset" value="重置"></td><td style="text-align:right"><input type="submit" value="提交"></td></tr>
        </table>
    </form>
    </div>
    </body>
    </html>
    

     

      

     

  • 相关阅读:
    money 和 smallmoney
    Sql server decimal 和 numeric
    SQL server数据类型int、bigint、smallint、tinyint
    c# 的传递参数值传递与传递引用的区别,ref与out区别
    释放SQL Server占用的内存
    JavaScript学习总结(一)——JavaScript基础
    js1
    Expected URL scheme 'http' or 'https' but no colon was found
    转载:SpringBoot Process finished with exit code 0
    转载:十大经典排序算法(动图演示)
  • 原文地址:https://www.cnblogs.com/phyger/p/9117866.html
Copyright © 2011-2022 走看看