您好,登錄后才能下訂單哦!
未使用{{useform}}而使用{{ userform.password }}形式便于后期css樣式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="." method="POST">
{% csrf_token %}
<div>username: <input type="text" name="username"> </div>
<div>password: {{ userform.password }}</div>
<div><input type="submit" value="注冊" name="reg"> <input type="submit" value="登陸" name="login"> </div>
</form>
</body>
</html>
#/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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。