python注册登录修改密码退出_django实现简单的登录,注册与修改密码

本文介绍了如何使用Django框架实现用户登录、注册及修改密码的功能。通过创建User模型,定义UserForm和ChangeForm表单,并在views.py中编写相关视图函数,实现了用户管理的基本操作。用户注册时检查用户名是否存在,登录时验证用户名和密码,修改密码时确认原密码正确性后再更新新密码。
摘要由CSDN通过智能技术生成

1:创建model

你的app/models.py

class User(models.Model):

phone=models.CharField(verbose_name='站名',max_length=100)

password=models.CharField(verbose_name='站名',max_length=100)

registTime = models.DateTimeField(max_length=250, verbose_name='加入时间',auto_now_add=True,blank=True)#     auto_now_add=True第一次注册创建的时间

loginTime = models.DateTimeField(max_length=250, verbose_name='最近登陆',auto_now=True,blank=True)# auto_now=True每次登录都会更新这个时间

2:创建view

你的app/views.py

from django.http import HttpResponse,HttpResponseRedirect

from django.shortcuts import render,render_to_response

from django import forms

from rnapp.models import User

##定义UserForm表单用于注册和登录页面,ChangeForm表单用于修改密码页面

class UserForm(forms.Form):

phone = forms.CharField(label='用户名')

password = forms.CharField(label='密  码',widget=forms.PasswordInput())

#last_login = forms.DateTimeField()

class ChangeForm(forms.Form):

phone = 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():

phone = uf.cleaned_data['phone']

password = uf.cleaned_data['password']

##判断用户原密码是否匹配

user = User.objects.filter(phone = phone)

if user:

info = '用户名已存在!'

elif len(user) == 0:

info = '注册成功!'

user = User()

user.phone = phone

user.password = password

user.save()

return HttpResponse(info)

else:

uf = UserForm()

return render(request, 'public/regist.html', locals())

def login(request):

if request.method == 'POST':

##获取表单信息

uf = UserForm(request.POST)

if uf.is_valid():

phone = uf.cleaned_data['phone']

password = uf.cleaned_data['password']

##判断用户密码是否匹配

user = User.objects.filter(phone = phone)

if user:

passwd = User.objects.filter(phone = phone, password = password)

if passwd:

info = '登录成功!'

else:

info = '请检查密码是否正确!'

elif len(user) == 0:

info = '请检查用户名是否正确!'

return HttpResponse(info)

else:

uf = UserForm()

return render(request,'public/login.html', locals())

def change_pass(request):

if request.method == 'POST':

uf = ChangeForm(request.POST)

if uf.is_valid():

phone = uf.cleaned_data['phone']

old_password = uf.cleaned_data['old_password']

new_password = uf.cleaned_data['new_password']

##判断用户原密码是否匹配

user = User.objects.filter(phone = phone)

if user:

passwd = User.objects.filter(phone = phone , password = old_password )

if passwd:

User.objects.filter(phone = phone,password = old_password).update(password = new_password)        ##如果用户名、原密码匹配则更新密码

info = '密码修改成功!'

else:

info = '请检查原密码是否输入正确!'

elif len(user) == 0:

info = '请检查用户名是否正确!'

return HttpResponse(info)

else:

uf = ChangeForm()

return render(request,'public/change.html',{'uf':uf})

def index(request):

return render(request, "index.html")

# Create your views here.

3:创建模板

你的app/template/public/regist.html

你的app/template/public/login.html

你的app/template/public/change.html

f12885906078

f12885906078

f12885906078

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值