创建数据库表
from django.db import models
# Create your models here.
class UserInfo(models.Model):
uername = models.CharField(verbose_name='用户名', max_length=32)
email = models.EmailField(verbose_name='邮箱', max_length=32)
mobile_phone = models.CharField(verbose_name='手机号', max_length=32)
password = models.CharField(verbose_name='密码', max_length=32)
生效数据库配置
python3 manage.py makemigrations
python3 manage.py migrate
创建视图函数
from django.shortcuts import render
import random
from django.shortcuts import HttpResponse
from utils.tencent.SMS import send_sms_single
from django.conf import settings
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError
from django import forms
from app01 import models
class RegisterModeForm(forms.ModelForm):
mobile_phone = forms.CharField(
label='手机号',validators=[RegexValidator(r'^(1[3|4|5|6|7|8|9])\d{9}$', '手机号格式错误'),],
widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': "请输入手机号"}))
password = forms.CharField(label='密码', widget=forms.PasswordInput(
attrs={'class': 'form-control', 'placeholder': "请输入密码"}))
confirm_password = forms.CharField(label='重复密码', widget=forms.PasswordInput(
attrs={'class': 'form-control', 'placeholder': "请重复输入密码"}))
code = forms.CharField(label='验证码',widget=forms.TextInput(
attrs={'class': 'form-control', 'placeholder': "请输入密码"}))
email = forms.EmailField(label='邮箱',widget=forms.EmailInput(
attrs={'class': 'form-control', 'placeholder': "请输入邮箱"}))
username = forms.CharField(label='用户名',widget=forms.TextInput(
attrs={'class': 'form-control', 'placeholder': "请输入用户名"}))
class Meta:
model = models.UserInfo
fields = ['username','email','password','confirm_password','mobile_phone','code']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for name, field in self.fields.items():
field.widget.attrs['class'] = 'form-control'
field.widget.attrs['placeholder'] = '请输入%s' %(field.label)
def register(request):
form = RegisterModeForm()
return render(request, 'register.html', {'form': form})
创建html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css">
<style>
.account{
width: 400px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="account">
<h1>注册</h1>
<form>
{% for field in form %}
{% if field.name == 'code' %}
<div class="form-group">
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
<div class="clearfix">
<div class="col-md-6" style="padding-left: 0;">{{ field }}</div>
<div class="col-md-6"><input type="button" class="btn btn-default" value="点击获取验证码"></div>
</div>
</div>
{% else %}
<div class="form-group">
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{{ field }}
</div>
{% endif %}
{% endfor %}
<button type="submit" class="btn btn-primary">注 册</button>
</form>
</div>
</body>
</html>
添加路由跳转
path('app01/register/', views.register),
访问测试
