Django之作业:相亲
Django之作业:相亲
环境:Python3.6、Django2.0.6、MySQL5.7、PyMySQL0.9
目录结构业务划分:
登录注销业务在views包中的account.py
相亲的男女匹配业务在love.py
-----各页面-----:
others.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% include "user_header.html" %} <h1>有关系的异性</h1> <ul> {% for row in user_list %} {% if row.g__nickname %} <li>{{ row.g__nickname }}</li> {% else %} <li>{{ row.b__nickname }}</li> {% endif %} {% endfor %} </ul> </body> </html>
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% include "user_header.html" %} <a href="/others/">查看和我有关的异性</a> <h3>异性列表</h3> <ul> {% for user in user_list %} <li>{{ user.nickname }}</li> {% endfor %} </ul> </body> </html>
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>登录</h1> <form method="post" action="login.html"> {% csrf_token %} <p>username:<input type="text" name="username"></p> <p>password:<input type="password" name="password"></p> <p>性别: 男:<input type="radio" name="gender" value="1"> 女:<input type="radio" name="gender" value="2"> </p> <p><input type="checkbox" name="rmb" value="11">一月免登陆</p> <input type="submit" value="submit">{{ msg }} </form> </body> </html>
user_header.html
<h1>用前用户:{{ request.session.user_info.nickname }}</h1> <a href="/logout/">注销</a>
-----各后端-----
url.py
from django.contrib import admin from django.conf.urls import url from app01.views import account from app01.views import love urlpatterns = [ # path('admin/', admin.site.urls), url('^login/', account.login), url('^logout/', account.logout), url('^index/', love.index), url('^others/', love.others), url('^test/', love.test), ]
models.py
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=32) gender_choices = ( (1,"man"), (2,"woman"), ) gender = models.IntegerField(choices=gender_choices) #用ManyToManyField建立boy和girl的关系 #ManyToManyField 的自关联 m = models.ManyToManyField("UserInfo") # 方式一: #related_query_name 的形式是:boys_set.all() #related_query_name 的意思是: 根据对象反向查找时用boys就可以啦,boys代表这个g的对象反向关联的Foreignkey # class U2U(models.Model): # g = models.ForeignKey("UserInfo",related_query_name="boys") # b = models.ForeignKey("UserInfo",related_query_name="girls") #方式二: #related_name 的形式是:boys.all() #related_name 的意思是: 根据对象反向查找时用boys就可以啦,boys代表这个g的对象反向关联的Foreignkey # class U2U(models.Model): # g = models.ForeignKey("UserInfo",related_name="boys",on_delete=models.CASCADE) # b = models.ForeignKey("UserInfo",related_name="girls",on_delete=models.CASCADE) class Comment(models.Model): """ 评论表 """ new_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="") # class Boy(models.Model): # nickname = models.CharField(max_length=32) # username = models.CharField(max_length=32) # password = models.CharField(max_length=64) # # class Girl(models.Model): # nickname = models.CharField(max_length=32) # username = models.CharField(max_length=32) # password = models.CharField(max_length=64) # # class B2G(models.Model): # b = models.ForeignKey(to="Boy",to_field="id",on_delete=models.CASCADE) # g = models.ForeignKey(to="Girl",to_field="id",on_delete=models.CASCADE) # Create your models here.
account.py
""" 登录相关 """ from django.shortcuts import redirect,HttpResponse,render from app01 import models def login(request): if request.method == "GET": return render(request,"login.html") else: username = request.POST.get("username") password = request.POST.get("password") gender = request.POST.get("gender") rmb = request.POST.get("rmb") #性别判断 if gender == "1": obj = models.Boy.objects.filter(username=username,password=password).first() print("account:Boy",obj.username,obj.password) else: obj = models.Girl.objects.filter(username=username,password=password).first() print("account:Girl", obj.username,obj.password) if not obj: return render(request,"login.html",{"msg":"username or password is error"}) else: print("111") # request.session["username_id"] = obj.id # request.session["gender"] = gender # request.session["username"] = username request.session["user_info"] = \ {"username_id":obj.id,"gender":gender,"username":username,"nickname":obj.nickname} print("222") return redirect("/index/") def logout(request): # request.session.delete(request.session.session_key) #删除数据库session存的相关的所以数据 request.session.clear() #clear只是清空了登录用户的当前cookie,也就是将cookie的时间设为了0. return redirect("/login/")
love.py
""" 相亲页面 """ from django.shortcuts import redirect,HttpResponse,render from app01 import models def index(request): if not request.session.get("user_info"): return redirect("/login/") else: #拿取当前登录用户的昵称,也可以直接在传到前端的request里获取 # current_nick_name = request.session.get("user_info").get("nickname") #男生查看女生列表,女生查看男生列表 #先辨别是男是女 gender = request.session.get("user_info").get("gender") print("gender",gender) if gender == "1": user_list = models.Girl.objects.all() print(user_list) else: user_list = models.Boy.objects.all() print(user_list) return render(request,"index.html",{"user_list":user_list}) def others(request): """ 获取与当前用户有关的异性 :param request: :return: """ current_username_id = request.session.get("user_info").get("username_id") #当前用户id gender = request.session.get("user_info").get("gender") if gender == "1": user_list = models.B2G.objects.filter(b_id=current_username_id).values("g__nickname") else: user_list = models.B2G.objects.filter(g_id=current_username_id).values("b__nickname") return render(request,"others.html",{"user_list":user_list}) def test(request): # #创建关系数据 # models.U2U.objects.create(b_id=1,g_id=3) # models.U2U.objects.create(b_id=4,g_id=3) # models.U2U.objects.create(b_id=4,g_id=2) # models.U2U.objects.create(b_id=4,g_id=5) # models.U2U.objects.create(b_id=1,g_id=5) #通过对象的形式给U2U添加关系数据 # boy = models.UserInfo.objects.filter(gender=1,id=1).first() # girl = models.UserInfo.objects.filter(gender=2,id=3).first() # models.U2U.objects.create(b=boy,g=girl) #方式二的ORM建表方式的取值: #love_list是UserInfo对象 # love_list = models.UserInfo.objects.filter(id=1).first() #拿取与love_list 中与id有关的数据,通过love_list的反向关联拿取 # result = love_list.girls.all() #result拿到的是U2U 的列表,列表里是U2U的对象 # for u in result: # print(u.g.nickname) #ManyToManyField 的方式的建表的取值方式: #ManyToManyField 的自管理取值: #查男生的方式: user_boy = models.UserInfo.objects.filter(id=4).first() love_list = user_boy.m.all() #love_list 是UserInfo 的对象 for row in love_list: print(row.id,row.gender,row.nickname) #查女生的方式: user_girl = models.UserInfo.objects.filter(id=2).first() love_list = user_girl.userinfo_set.all() #与女孩相关的男孩个数 for row in love_list: print(row.id,row.gender,row.nickname) return HttpResponse("...")
love_list 反向关联查找拿到的是U2U的对象:
-------- END --------