Django登录 注册和修改密码简单页面实现

首先给大家推荐一下我老师大神的人工智能教学网站。教学不仅零基础,通俗易懂,而且非常风趣幽默,还时不时有内涵黄段子!点这里可以跳转到网站

1、创建Model

userpw/models.py

from django.db import modelsimport hashlib# Create your models here. class User(models.Model):    username = models.CharField(max_length=30)    password = models.CharField(max_length=50)    last_login = models.DateTimeField(blank=True)     def __unicode__(self):        return self.username

2、创建View

userpw/views.py

# -*- coding: utf-8 -*-from django.http import HttpResponse,HttpResponseRedirectfrom django.shortcuts import render_to_response ,RequestContextfrom django import formsfrom userpw.models import User # Create your views here.##定义了UserForm表单用于注册和登录页面,ChangeForm表单用于修改密码页面class UserForm(forms.Form):    username = forms.CharField(label='用户名')    password = forms.CharField(label='密   码',widget=forms.PasswordInput())    #last_login = forms.DateTimeField() class ChangeForm(forms.Form):    username = forms.CharField(label='用户名')    old_password = forms.CharField(label='原密码',widget=forms.PasswordInput())    new_password = forms.CharField(label='新密码',widget=forms.PasswordInput())  def regist(request):    if request.method == 'POST':        uf = UserForm(request.POST)        if uf.is_valid():            username = uf.cleaned_data['username']            password = uf.cleaned_data['password']             ##判断用户原密码是否匹配            user = User.objects.filter(username = username)            if user:                info = '用户名已存在!'            elif len(user) == 0:                info = '注册成功!'                user = User()                user.username = username                user.password = password                user.save()             return HttpResponse(info)    else:        uf = UserForm()     return render_to_response('regist.html', {'uf': uf}) def login(request):    if request.method == 'POST':        ##获取表单信息        uf = UserForm(request.POST)        if uf.is_valid():            username = uf.cleaned_data['username']            password = uf.cleaned_data['password']             ##判断用户密码是否匹配            user = User.objects.filter(username = username)            if user:                passwd = User.objects.filter(username = username, password = password)                if passwd:                    info = '登录成功!'                else:                    info = '请检查密码是否正确!'            elif len(user) == 0:                info = '请检查用户名是否正确!'             return HttpResponse(info)    else:        uf = UserForm()     return render_to_response('login.html', {'uf': uf}) def change_pass(request):    if request.method == 'POST':        uf = ChangeForm(request.POST)        if uf.is_valid():            username = uf.cleaned_data['username']            old_password = uf.cleaned_data['old_password']            new_password = uf.cleaned_data['new_password']             ##判断用户原密码是否匹配            user = User.objects.filter(username = username)            if user:                passwd = User.objects.filter(username = username , password = old_password )                if passwd:                    User.objects.filter(username = username,password = old_password).update(password = new_password)        ##如果用户名、原密码匹配则更新密码                    info = '密码修改成功!'                else:                    info = '请检查原密码是否输入正确!'            elif len(user) == 0:                info = '请检查用户名是否正确!'         return HttpResponse(info)    else:        uf = ChangeForm()    return render_to_response('change.html',{'uf':uf})

3、创建Template模板

userpw/template/regist.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />    <title>regist</title></head><body><h1>regist</h1><form method="post" enctype="multipart/form-data" >{{uf.as_p}}<input type="submit" value="注册"/></form></body></html>

userpw/template/login.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />    <title>login</title></head><body><h1>login</h1><form method="post" enctype="multipart/form-data" >{{uf.as_p}}<input type="submit" value="登录"/></form></body></html>

userpw/template/change.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />    <title>Change Password</title></head><body><h1>Change Password</h1><form method="post" enctype="multipart/form-data" >{{uf.as_p}}<input type="submit" value="修改密码"/></form></body></html>

点这里可以跳转到人工智能网站

发表评论