HTML form是交互网页的支柱。下面来学习一下使用Django自带 Form表单编写一个简单的注册登录小程序。
一、项目结构如下图:
re_login是app名
二、编写代码:
1、app下models.py:
#coding:utf-8 from django.db import models # Create your models here. class Register(models.Model): username = models.CharField(max_length = 32,verbose_name = '用户名') password = models.CharField(max_length = 32,verbose_name = '密码') email = models.EmailField(verbose_name = '注册邮箱') phone_number = models.CharField(max_length = 14,verbose_name = '电话号码')2、在app下创建一个form.py文件:
#coding:utf-8 from django import forms class UserRegister(forms.Form): username = forms.CharField(label='注册用户名', max_length=100) password1 = forms.CharField(label='设置密码', widget=forms.PasswordInput()) password2 = forms.CharField(label='确认密码', widget=forms.PasswordInput()) email = forms.EmailField(label='电子邮件') phone_number = forms.CharField(label='手机号码',max_length=15) class UserLogin(forms.Form): username = forms.CharField(label='用户名', max_length=100) password = forms.CharField(label='密码', widget=forms.PasswordInput())
3、app下views.py:
#coding:utf-8 from django.shortcuts import render,render_to_response import hashlib from models import Register from django.template import RequestContext from form import UserLogin,UserRegister # Create your views here. def take_md5(content): hash = hashlib.md5() #创建hash加密实例 hash.update(content) #hash加密 result = hash.hexdigest() #得到加密结果 return result #注册 def register(request): if request.method == 'POST': form = UserRegister(request.POST) if form.is_valid(): #获取表单信息 username = form.cleaned_data['username'] namefilter = Register.objects.filter(username = username) if len(namefilter) > 0: return render_to_response('register.html',{'error':'用户名已存在'}, context_instance=RequestContext(request)) else: password1 = form.cleaned_data['password1'] password2 = form.cleaned_data['password2'] if password1 != password2: return render_to_response('register.html',{'error':'两次输入的密码不一致!'}, context_instance=RequestContext(request)) else: password = take_md5(password1) email = form.cleaned_data['email'] phone_number = form.cleaned_data['phone_number'] #将表单写入数据库 user = Register.objects.create(username=username,password=password,email=email, phone_number=phone_number) user.save() return render_to_response('success.html',{'username':username,'operation':'注册'}, context_instance=RequestContext(request)) else: form = UserRegister() return render_to_response('register.html',{'form':form},context_instance=RequestContext(request)) #登录 def login(request): if request.method == 'POST': form = UserLogin(request.POST) if form.is_valid(): #获取表单信息 username = form.cleaned_data['username'] password = form.cleaned_data['password'] password = take_md5(password) namefilter = Register.objects.filter(username=username,password=password) if len(namefilter) > 0: return render_to_response('success.html',{'username':username,'operation':'登录'}, context_instance=RequestContext(request)) else: return render_to_response('login.html',{'error':'该用户名不存在!'}, context_instance=RequestContext(request)) else: form =UserLogin() return render_to_response('login.html',{'form':form}, context_instance=RequestContext(request))
4、urls.py:
....... from re_login.views import register,login urlpatterns = [ ....... url(r'^$',login), url(r'^login/', login), url(r'^register/', register), ]
5、login.html:
6、register.html:
7、success.html:
恭喜你,{{ operation }}成功 href="http://127.0.0.1:8000/login/">返回登录页面
至此,一个能实现简单注册登录功能的完整代码就完成了,页面的样式可以自己看着写