Day_05

1. 点击注册

1.1 点击收集数据&ajax

$.ajax({
                    url: "{% url 'register' %}",
                    type: "POST",
                    data: $('#regForm').serialize(),
                    dataType: "JSON",
                    success: function (res) {
                        console.log(res);
                    }
                })

1.2 数据校验(每个字段)

点击查看代码
# 创建每个字段的钩子函数,进行校验
    def clean_username(self):
        username = self.cleaned_data["username"]
        exists = models.UserInfo.objects.filter(username=username).exists()
        if exists:
            raise ValidationError("用户名已存在")
        return username
    def clean_email(self):
        email = self.cleaned_data["email"]
        exists = models.UserInfo.objects.filter(email=email).exists()
        if exists:
            raise ValidationError("该邮箱已被使用")
        return email
    def clean_password(self):
        pwd = self.cleaned_data["password"]
        #加密并返回
        return md5(pwd)
    def clean_confirm_password(self):
        pwd = self.cleaned_data["password"]
        confirm_pwd = md5(self.cleaned_data["confirm_password"])
        if pwd != confirm_pwd:
            raise ValidationError("密码不一致")
        return confirm_pwd
    def clean_mobile_phone(self):
        mobile_phone = self.cleaned_data["mobile_phone"]
        exists = models.UserInfo.objects.filter(mobile_phone=mobile_phone).exists()
        if exists:
            raise ValidationError("手机号已注册")
        return mobile_phone
    def clean_code(self):
        code = self.cleaned_data["code"]
        mobile_phone = self.cleaned_data["mobile_phone"]
        conn = get_redis_connection()
        redis_code = conn.get(mobile_phone)
        if not redis_code:
            raise ValidationError("验证码失效或未发送,请重新发送")
        redis_str_code = redis_code.decode('utf-8')
        if redis_str_code != code.strip():
            raise ValidationError("输入验证码不正确")
        return code

1.3 写入数据库

image

2. 短信登录

2.1 展示页面

创建form类

class LoginSmsForm(BootStrapForm,forms.Form):
    mobile_phone = forms.CharField(
        label='手机号',
        validators=[RegexValidator(r'^(1[3|4|5|6|7|8|9])\d{9}$', '手机格式错误'), ]
    )
    code = forms.CharField(
        label='验证码',
        widget=forms.TextInput()
    )

创建视图函数login_sms

def login_sms(request):
    '''短信登录'''
    form = LoginSmsForm()
    return render(request,"login_sms.html",{"form":form})

login_sms.html

点击查看代码
{% extends 'layout/basic.html' %}
{% load static %}
{% block title %} 用户短信登录 {% endblock %}

{% block css %}

    <link rel="stylesheet" href="{% static 'css/account.css' %}">

{% endblock %}

{% block content %}
    <div class="account">
        <div class="title">用户短信登录</div>
        <form id="smsForm" method="post" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_lable }}">{{ field.label }}</label>
                        <div class="row">
                            <div class="col-xs-7" style="padding-left: 0px;">
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>
                            <div class="col-xs-5">
                                <input id="btnSms" type="button" class="btn btn-info" value="点击获取验证码">
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_lable }}">{{ field.label }}</label>
                        <div class="row">
                            {{ field }}
                            <span class="error-msg"></span>
                        </div>
                    </div>
                {% endif %}
            {% endfor %}

            <div class="row">
                <div class="col-xs-3" style="padding-left: 0px;">
                    <input id="btnSubmit" type="button" class="btn btn-primary" value="登  录"></input>
                </div>
            </div>

        </form>
    </div>

{% endblock %}

image

2.2 点击发送短信

和注册相同的发送ajax请求,结果如下:
image

2.3 点击登录(form中的钩子函数验证)

image

posted @ 2023-05-04 08:47  Cool-Bo  阅读(16)  评论(0)    收藏  举报