您好,登錄后才能下訂單哦!
1,創建兩個頁面,一個是登錄頁面,一個是登錄成功后,顯示個人信息的頁面
2,登錄頁面是基于session
3、數據表(男生,女生,男女關系)
4、登錄頁(用戶名,密碼,性別,一周免登陸)
5、內容頁,異性列表和有關系的列表
把登陸的代碼和關系內容的代碼分開,所以創建了views目錄,并且把不同的內容寫到了不同的模塊中。如圖
各個py文件代碼
from django.shortcuts import render,HttpResponse,redirect
from app_01 import models
def acc_login(request):
if request.method == "GET":
return render(request,'acclogin.html')
else:
user = request.POST.get('username')
gender = request.POST.get('gender')
pwd = request.POST.get('password')
rmb = request.POST.get('rmb')
print(user,gender,pwd,rmb)
if gender == "1":
obj = models.AccBoy.objects.filter(username=user,password=pwd).first()
else:
obj = models.AccGirl.objects.filter(username=user,password=pwd).first()
if not obj:
return render(request,'acclogin.html',{'msg':'用戶名或密碼錯誤'})
else:
request.session['user_info'] = {'user_d':obj.id,'gender':gender,'rmb':rmb,'user':user,'nickname':obj.nickname}
return redirect('/accindex.html')
def acclogout(request):
if request.session.get('user_info'):
request.session.clear()
return redirect('/acclogin.html')
from django.shortcuts import render,redirect,HttpResponse
from app_01 import models
def accindex(requtst):
if not requtst.session.get('user_info'):
return redirect('/acclogin.html')
else:
gender = requtst.session.get("user_info").get('gender')
if gender == '1':
user_list = models.AccGirl.objects.all()
else:
user_list = models.AccBoy.objects.all()
return render(requtst,'accindex.html',{'user_list':user_list})
accindex
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>accindex</title>
</head>
<body>
<h3>當前用戶:{{ request.session.user_info.nickname }}</h3>
<p><a href="/acclogin.html">注銷用戶</a></p>
<h3>用戶列表</h3>
<hr>
<ul>
{% for item in user_list %}
<li>{{ item.username }}</li>
{% endfor %}
</ul>
</body>
</html>
acclogin代碼
<form action="/acclogin.html" method="post">
{% csrf_token %}
<p><input type="text" name="username">用戶名</p>
<p><input type="password" name="password">密碼</p>
<input type="radio" name="gender" value="1">男
<input type="radio" name="gender" value="2">女
<input type="checkbox" value="11" name="rmb" />一周內免登陸
<p><input type="submit" value="提交"></p>{{ msg }}
</form>
from django.db import models
class UserInfo(models.Model):
nickname = models.CharField(max_length=32)
username = models.CharField(max_length=32)
password = models.CharField(max_length=64)
gender_choices = (
(1,'男'),
(2,'女'),
)
gender = models.IntegerField(choices=gender_choices)
m = models.ManyToManyField('UserInfo')
"""
1 女
2 女
3 女
4 男
5 男
a1 = 2
a2 = 5
"""
# related_query_name
# obj對象男.b_set.all()
# obj對象女.a_set.all()
# related_name
# obj對象男.a.all()
# obj對象女.b.all()
# class U2U(models.Model):
# g = models.ForeignKey('UserInfo',related_name='boys')
# b = models.ForeignKey('UserInfo',related_name='girls')
# U2U.objects.create(g_id=a1,b_id=a2)
# related_query_name,related_name
# class UserType(models.Model):
# title = models.CharField(max_length=32)
#
# class User(models.Model):
# username = models.CharField(max_length=32)
# ut = models.ForeignKey('UserType',related_query_name='xxxx')
# 反向:
# related_name='xxxx'
# user_set ==> xxxx
# related_query_name='xxxx'
# user_set ==> xxxx_set
# FK自關聯
class Comment(models.Model):
"""
評論表
"""
news_id = models.IntegerField() # 新聞ID
content = models.CharField(max_length=32) # 評論內容
user = models.CharField(max_length=32) # 評論者
reply = models.ForeignKey('Comment',null=True,blank=True,related_name='xxxx')
"""
新聞ID reply_id
1 1 別比比 root null
2 1 就比比 root null
3 1 瞎比比 shaowei null
4 2 寫的正好 root null
5 1 拉倒吧 由清濱 2
6 1 拉倒吧1 xxxxx 2
7 1 拉倒吧2 xxxxx 5
"""
"""
新聞1
別比比
就比比
- 拉倒吧
- 拉倒吧2
- 拉倒吧1
瞎比比
新聞2:
寫的正好
"""
# def test(request):
# # models.U2U.objects.create(b_id=2,g_id=6)
# # models.U2U.objects.create(b_id=1,g_id=6)
# # models.U2U.objects.create(b_id=1,g_id=4)
# # models.U2U.objects.create(b_id=1,g_id=5)
#
# # boy = models.UserInfo.objects.filter(gender=1,id=2).first()
# # girl = models.UserInfo.objects.filter(gender=2,id=6).first()
# # models.U2U.objects.create(b=boy,g=girl)
#
# # UserInfo對象
# # xz = models.UserInfo.objects.filter(id=1).first()
# # # 和徐崢有關系的所有信息:U2U列表[U2U對象,2U對象,2U對象,]
# # result = xz.girls.all()
# # for u in result:
# # # U2U對象
# # print(u.g.nickname)
#
# # 查男生
# # xz = models.UserInfo.objects.filter(id=1).first()
# # u = xz.m.all()
# # for row in u:
# # print(row.nickname)
# # 查女神
# # xz = models.UserInfo.objects.filter(id=4).first()
# # v = xz.userinfo_set.all()
# # for row in v:
# # print(row.nickname)
# return HttpResponse('...')
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。