欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Django实现表单验证

程序员文章站 2023-12-24 19:13:09
本文实例为大家分享了django实现表单验证的具体代码,供大家参考,具体内容如下 models.py class users(models.model):...

本文实例为大家分享了django实现表单验证的具体代码,供大家参考,具体内容如下

models.py

class users(models.model):
  nickname = models.charfield(max_length=16, null=false, blank=false, unique=true)
  email = models.emailfield(max_length=32, null=false, blank=false, unique=true)
  password = models.charfield(max_length=64, null=false, blank=false)
  head = models.imagefield(default="decault.png")
  age = models.charfield(max_length=3,blank=true,null=true)
  sex = models.charfield(max_length=2, blank=true, null=true)
  isactivate = models.booleanfield(default=false)

  def save(self):
    if not self.password.startswith('pbkdf2_'):
      self.password = make_password(self.password)
    super().save()

form.py

from django import forms
from django.core.exceptions import validationerror

from user.models import users

#定义验证器
def nickname_validate(nickname):
  u = users.objects.filter(nickname=nickname)
  if len(u):
    print(len(u))
    raise validationerror('用户名已存在')

#定义表单
class registerform(forms.form):
  nickname = forms.charfield(validators=[nickname_validate],
                label='用户名',
                max_length=16,
                min_length=4,
                required=true,
                widget= forms.textinput(),
                )

  password = forms.charfield(label='密码',
                max_length=64,
                min_length=6,
                required=true,
                widget=forms.passwordinput())

  email = forms.emailfield(label='邮箱',
               max_length=32,
               required=true)

  age = forms.charfield(label='年龄',
             max_length=3,
             required=false)

  sex = forms.choicefield(label='性别',
              choices = ((0,'男'),(1,'女'),),
              required=false)

view.py

from user.forms import registerform
from user.models import users

def register(request):
  if request.method == 'post':
    form = registerform(request.post)
    if form.is_valid():
      u = users()
      u.nickname = form.cleaned_data['nickname']
      u.email = form.cleaned_data['email']
      u.password = form.cleaned_data['password']
      u.age = form.cleaned_data['age']
      u.sex = form.cleaned_data['sex']
      u.save()
      return render(request,'user_info.html')
    else:
      return render(request, 'register.html',context={'form':form,'errors': form.errors})
  else:
    form = registerform()
  return render(request,'register.html',context={'form':form})

register.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>注册</title>
</head>
<body>
  <form class="form" action="{% url 'user:register' %}" method="post">
    {% csrf_token %}
    <table>
      {{ form.as_p }}
    </table>
    <button type="submit" class="btn btn-primary btn-block">注册
    </button>
    <input type="hidden" name="next" value="{{ next }}"/>
  </form>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: