欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django實(shí)現(xiàn)表單驗(yàn)證

 更新時(shí)間:2018年09月08日 09:21:39   作者:不凡De老五  
這篇文章主要為大家詳細(xì)介紹了Django實(shí)現(xiàn)表單驗(yàn)證的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Django實(shí)現(xiàn)表單驗(yàn)證的具體代碼,供大家參考,具體內(nèi)容如下

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

#定義驗(yàn)證器
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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python?pyecharts案例超市4年數(shù)據(jù)可視化分析

    Python?pyecharts案例超市4年數(shù)據(jù)可視化分析

    這篇文章主要介紹了Python?pyecharts案例超市4年數(shù)據(jù)可視化分析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • 使用Python向C語言的鏈接庫傳遞數(shù)組、結(jié)構(gòu)體、指針類型的數(shù)據(jù)

    使用Python向C語言的鏈接庫傳遞數(shù)組、結(jié)構(gòu)體、指針類型的數(shù)據(jù)

    今天小編就為大家分享一篇關(guān)于使用Python向C語言的鏈接庫傳遞數(shù)組、結(jié)構(gòu)體、指針類型的數(shù)據(jù),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 最新評論