未使用{{useform}}而使用{{ userform.password }}形式便于后期css样式
Title
#/usr/bin/env python3
from django import forms
class UserForm(forms.Form):
username=forms.CharField()
password=forms.CharField()
from django.db import models
class User(models.Model):
username=models.CharField(max_length=30)
password=models.CharField(max_length=30)
def __str__(self):
return self.username
from django.shortcuts import render
from django.views.decorators.cache import cache_page
from .models import User
# Create your views here.
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from djapp.forms import UserForm
def user(request):
if request.method == 'POST':
userform=UserForm(request.POST)
if userform.is_valid():
if request.POST.get('reg'):
username=userform.cleaned_data['username']
password=userform.cleaned_data['password']
User.objects.create(username=username,password=password)
# user=User()
# user.username=username
# user.password=password
# user.save()
return HttpResponse("login 注册%s"%username)
else:
if request.session.get('user_id') == 3:
return HttpResponse("session 登陆")
else:
try:
user=User.objects.get(username=request.POST.get('username',None))
except Exception as e:
print(e)
return HttpResponse("找不到用户")
if user.password == request.POST.get('password'):
request.session['user_id']=user.id
return HttpResponse("登陆成功")
else:
return HttpResponse("登陆失败")
else:
userform=UserForm()
return render(request,'djapp/user.html',{'userform':userform})
cookie保存sessionid
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。