DjangoORM多对多案例!
一、数据准备
from django.db import models # Create your models here. class Boy(models.Model): nickname = models.CharField(max_length=32) username = models.CharField(max_length=32) password = models.CharField(max_length=63) class Girl(models.Model): nickname = models.CharField(max_length=32) username = models.CharField(max_length=32) password = models.CharField(max_length=63) class B2G(models.Model): b = models.ForeignKey(Boy, on_delete=models.CASCADE) g = models.ForeignKey(Girl, on_delete=models.CASCADE)
from django.http import HttpResponse from app01 import models def init_models(request): # 管理员验证,保证数据安全性 # 略 girls = [ models.Girl(nickname=\'娜美\', username=\'namei\', password=\'123\'), models.Girl(nickname=\'老幺\', username=\'laoyao\', password=\'123\'), models.Girl(nickname=\'女帝\', username=\'nvdi\', password=\'123\') ] models.Girl.objects.bulk_create(girls, 10) boys = [ models.Boy(nickname=\'路飞\', username=\'lufei\', password=\'123\'), models.Boy(nickname=\'柒禾\', username=\'qihe\', password=\'123\'), models.Boy(nickname=\'索隆\', username=\'zoro\', password=\'123\') ] models.Boy.objects.bulk_create(boys, 10) love = [ models.B2G(b_id=\'1\', g_id=\'1\'), models.B2G(b_id=\'1\', g_id=\'3\'), models.B2G(b_id=\'2\', g_id=\'2\'), models.B2G(b_id=\'3\', g_id=\'2\'), models.B2G(b_id=\'2\', g_id=\'3\'), ] models.B2G.objects.bulk_create(love, 10) return HttpResponse(\'添加数据成功\')
二、前端页面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/app01/myLogin.html" method="POST"> {% csrf_token %} <p>用户名:<input type="text" name="username"></p> <p>密码: <input type="password" name="password"></p> <p> 性别: 男 <input type="radio" name="gender" value="1"> 女 <input type="radio" name="gender" value="2"> </p> <input type="submit" value="提交">{{ msg }} <p><input type="checkbox" name="rmb" value="True">一个免登陆</p> </form> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% include \'user_header.html\' %} <h3>异性列表</h3> <a href="/app01/others.html">查看和我有关系的异性</a> <ul> {% for row in user_list %} <li>{{ row.nickname }}</li> {% endfor %} </ul> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% include \'user_header.html\' %} <p>其他异性</p> <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>
<h1>当前用户:{{ request.session.user_info.nickname }}</h1> <h1>{{ request.session.user_info }}</h1> <a href="/logout.html">注销</a>
三、路由系统
"""day74 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path(\'\', views.home, name=\'home\')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path(\'\', Home.as_view(), name=\'home\')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path(\'blog/\', include(\'blog.urls\'))
"""
from django.contrib import admin
from django.urls import path, re_path, include
from day74 import views
import app01
urlpatterns = [
# path(\'admin/\', admin.site.urls),
# path(\'test/\', views.test),
# path(\'test1/\', views.test_ajax),
# re_path(r\'^login$\', views.login),#显示ajax登录页面
# re_path(r\'^login_ajax_check$\', views.login_ajax_check),#显示ajax登录校验
path(\'app01/\', include(\'app01.urls\')),
]
"""day74 URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/3.1/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path(\'\', views.home, name=\'home\') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path(\'\', Home.as_view(), name=\'home\') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path(\'blog/\', include(\'blog.urls\')) """ from django.urls import path from app01.views import account, love, init_models urlpatterns = [ path(\'myLogin.html\', account.myLogin), path(\'others.html\', love.others), path(\'index.html\', love.index), path(\'init_models.html\', init_models.init_models) ]
四、视图函数
from django.http import HttpResponse from django.shortcuts import render, redirect from app01 import models def myLogin(request): if request.method == \'GET\': return render(request, \'myLogin.html\') else: user = request.POST.get(\'username\') pwd = request.POST.get(\'password\') gender = request.POST.get(\'gender\') rmb = request.POST.get(\'rmb\') print(user, pwd, gender) # 获取用户对象 if gender == \'1\': obj = models.Boy.objects.filter(username=user, password=pwd).first() else: obj = models.Girl.objects.filter(username=user, password=pwd).first() if not obj: # 没有登录成功 return render(request, \'myLogin.html\', {\'msg\': \'用户名或者密码错误\'}) else: request.session[\'user_info\'] = {\'user_id\': obj.id, \'gender\': gender, \'username\': user, \'nickname\': obj.nickname} return redirect(\'/app01/index.html\')
from django.shortcuts import redirect, render from app01 import models def index(request): if not request.session.get(\'user_info\'): return redirect(\'/login.html\') else: gender = request.session.get(\'user_info\').get(\'gender\') if gender == \'1\': user_list = models.Girl.objects.all() else: user_list = models.Boy.objects.all() return render(request, \'index.html\', {\'user_list\': user_list}) def others(request): current_user_id = request.session.get(\'user_info\').get(\'user_id\') gender = request.session.get(\'user_info\').get(\'gender\') if gender == \'1\': user_list = models.B2G.objects.filter(b_id=current_user_id).values(\'g__nickname\') else: user_list = models.B2G.objects.filter(g_id=current_user_id).values(\'b__nickname\') print(\'result\', user_list) return render(request, \'others.html\', {\'user_list\': user_list})
from django.http import HttpResponse from app01 import models def init_models(request): # 管理员验证,保证数据安全性 # 略 girls = [ models.Girl(nickname=\'娜美\', username=\'namei\', password=\'123\'), models.Girl(nickname=\'老幺\', username=\'laoyao\', password=\'123\'), models.Girl(nickname=\'女帝\', username=\'nvdi\', password=\'123\') ] models.Girl.objects.bulk_create(girls, 10) boys = [ models.Boy(nickname=\'路飞\', username=\'lufei\', password=\'123\'), models.Boy(nickname=\'柒禾\', username=\'qihe\', password=\'123\'), models.Boy(nickname=\'索隆\', username=\'zoro\', password=\'123\') ] models.Boy.objects.bulk_create(boys, 10) love = [ models.B2G(b_id=\'1\', g_id=\'1\'), models.B2G(b_id=\'1\', g_id=\'3\'), models.B2G(b_id=\'2\', g_id=\'2\'), models.B2G(b_id=\'3\', g_id=\'2\'), models.B2G(b_id=\'2\', g_id=\'3\'), ] models.B2G.objects.bulk_create(love, 10) return HttpResponse(\'添加数据成功\')