码迷,mamicode.com
首页 > 其他好文 > 详细

from 的使用

时间:2019-12-10 00:29:39      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:set   inf   必须   163邮箱   min   har   jquer   self   rms   

使用form实现登录

views
from django.shortcuts import render,redirect,HttpResponse
from app01 import models
from app01 import myform
from utils import md5_tool
# Create your views here.
def login(request):
    if request.method=='GET':
        return render(request,'auth/login.html')
    else:
        username=request.POST.get('username')
        password=request.POST.get('password')
        user_obj=models.Userinfo.objects.filter(username= username,password=password)
        if user_obj:
            return redirect('home')
        else:
            return render(request,'auth/login.html',{'errors':'用户密码有误'})



def register(request):
    if request.method=='GET':
        form_obj = myform.UserInForm()
        return render(request, 'auth/register.html', {'form_obj': form_obj})
    else:
        form_obj = myform.UserInForm(request.POST)
        if form_obj.is_valid():
            new_data = form_obj.cleaned_data
            new_data.pop('r_password')
            new_data['password'] = md5_tool.set_md5(new_data['password'], new_data['username'])
            models.Userinfo.objects.create(
                **new_data
            )
            return redirect('login')
        else:
            return render(request, 'auth/register.html', {'form_obj': form_obj})


def home(request):
    return render(request,'home.html')


myform.py

from app01 import models
from django import forms
from django.core.exceptions import ValidationError

#自定义
def mobile_validate(value):
    mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
    if not mobile_re.match(value):
        raise ValidationError('手机号码格式错误')
#form组件
class UserInForm(forms.Form):
    username = forms.CharField(
        min_length=2,
        widget=forms.TextInput(attrs={'class': 'username', 'placeholder': '您的用户名', 'autocomplete': 'off', }),
        error_messages={
            'min_length': '最短不能小于2个字符',
            'required': '用户名不能为空!'
        }

    )
    password = forms.CharField(
        min_length=6,
        widget=forms.PasswordInput(attrs={'class': 'password', 'placeholder': '输入密码', 'oncontextmenu': 'return false',
                                          'onpaste': 'return false', }),
    )
    r_password = forms.CharField(

        min_length=6,
        widget=forms.PasswordInput(attrs={'class': 'password', 'placeholder': '再次输入密码', 'oncontextmenu': 'return false',
                                          'onpaste': 'return false', }),
        error_messages={
            'min_length': '最短不能小于6个字符',
            'required': '确认密码不能为空!'
        }
    )


    telephone = forms.CharField(
        min_length=11,
        max_length=11,
        validators=[mobile_validate, ],
        error_messages={
            'required':'手机号不能为空!!'
        },
    widget = forms.TextInput(attrs={'placeholder': '请输入手机号'})

    )

    email = forms.CharField(
        widget=forms.TextInput(attrs={'placeholder': '请输入邮箱'}),
        error_messages={
            'required': '邮箱不能为空!'
        },
    )
    #局部钩子
    def clean_username(self):
        value = self.cleaned_data.get('username')
        ret=models.Userinfo.objects.filter(
            username=value,
        )
        if ret:
            raise ValidationError('用户名已经存在')
        else:
            return value

    # 局部钩子
    def clean_telephone(self):
         value=self.cleaned_data.get('telephone')
         if len(value) !=11:
             raise ValidationError('请输入11位手机号')
         else:
             return value

    # 局部钩子
    def clean_email(self):
         value=self.cleaned_data.get('email')
         ret=re.compile(r'\w+@163.com$')
         if ret.match(value):
             return value
         else:
             raise ValidationError('必须是163邮箱!')

    # 全局钩子
    def clean(self):
        p1 = self.cleaned_data.get('password')
        p2 = self.cleaned_data.get('r_password')

        if p1 == p2:
            return self.cleaned_data
        else:
            self.add_error('r_password', '两次密码不一致!')

register.html

{% load static %}
<!DOCTYPE html>
<!-- saved from url=(0063)http://www.jq22.com/demo/jquery-Sharelink20151012/register.html -->
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>注册丨Sharelink</title>
    <link rel="stylesheet" href="{% static 'auth/css/style.css' %}">
</head>
<body>
<div class="register-container">
    <h1>ShareLink</h1>
    <div class="connect">
        <p style="">Link the world. Share to world.</p>
    </div>
    <form action="{% url 'register' %}" method="post" id="registerForm"
          novalidate="novalidate">
        {% csrf_token %}
        <div>
            {{ form_obj.username }}
            <span style="color:red;font-size: 12px;">{{ form_obj.username.errors.0 }}</span>
        </div>
        <div>
            {{ form_obj.password }}
            <span style="color:red;font-size: 12px;">{{ form_obj.password.errors.0 }}</span>

            <!--oncontextmenu="return false" 标签上不能点击右键  onpaste="return false"不能往标签上粘贴内容  autocomplete="off"取消历史记录提示-->
        </div>
        <div>
            {{ form_obj.r_password }}
            <span style="color:red;font-size: 12px;">{{ form_obj.r_password.errors.0 }}</span>
        </div>
        <div>
            {{ form_obj.telephone }}
            <span style="color:red;font-size: 12px;">{{ form_obj.telephone.errors.0 }}</span>
        </div>
        <div>
            {{ form_obj.email }}
            <span style="color:red;font-size: 12px;">{{ form_obj.email.errors.0 }}</span>
        </div>
        <button id="submit" type="submit">注 册</button>
    </form>
    <a href="{% url 'login' %}">
        <button type="button" class="register-tis">已经有账号?</button>
    </a>
</div>
<script src="{% static 'auth/js/jquery.min.js' %}"></script>
{#<script src="{% static 'auth/js/jquery.min.js' %}"></script>#}
<script src="{% static 'auth/js/common.js' %}"></script>

<script src="{% static 'auth/js/supersized.3.2.7.min.js' %}"></script>
<script src="{% static 'auth/js/supersized-init.js' %}"></script>

<script src="{% static 'auth/js/jquery.validate.min.js' %}"></script>
</body>
</html>

login.html

{% load static %}
<!DOCTYPE html>
<!-- saved from url=(0050)http://www.jq22.com/demo/jquery-Sharelink20151012/ -->
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>登陆丨Sharelink</title>
    <link rel="stylesheet" href="{% static 'auth/css/style.css' %}">
</head>
<body>
<div class="login-container">
    <h1>ShareLink</h1>
    <div class="connect">
        <p style="">Link the world. Share to world.</p>
    </div>
    <form action="{% url 'login' %}" method="post" id="loginForm"
          novalidate="novalidate">
        {% csrf_token %}
        <div style="color:red;font-size: 12px;">{{ errors }}</div>
        <div>
            <input type="text" name="username" class="username" placeholder="用户名" autocomplete="off">
        </div>
        <div>
            <input type="password" name="password" class="password" placeholder="密码" oncontextmenu="return false" onpaste="return false">
        </div>
        <button id="submit" type="submit">登 陆</button>
    </form>
    <a href="{% url 'register' %}">
        <button type="button" class="register-tis">还有没有账号?</button>
    </a>
</div>

<script src="{% static 'auth/js/jquery.min.js' %}"></script>
{#<script src="{% static 'auth/js/jquery.min.js' %}"></script>#}
<script src="{% static 'auth/js/common.js' %}"></script>

<script src="{% static 'auth/js/supersized.3.2.7.min.js' %}"></script>
<script src="{% static 'auth/js/supersized-init.js' %}"></script>

<script src="{% static 'auth/js/jquery.validate.min.js' %}"></script>


</body>
</html>

from 的使用

标签:set   inf   必须   163邮箱   min   har   jquer   self   rms   

原文地址:https://www.cnblogs.com/x-h-15029451788/p/12014114.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!