django(相亲网站)
一.django设置
""" Django settings for day67xqwz project. Generated by 'django-admin startproject' using Django 1.10.6. For more information on this file, see https://docs.djangoproject.com/en/1.10/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.10/ref/settings/ """ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'xvx10ikw-i6yzua7aapvxtqlakjj7!lbvrbbk^8ce+a#bp#lf-' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app01.apps.App01Config', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'day67xqwz.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')] , 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'day67xqwz.wsgi.application' # Database # https://docs.djangoproject.com/en/1.10/ref/settings/#databases # # DATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), # } # } DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'xqwz', 'USER': 'root', 'PASSWORD': '', 'HOST': 'localhost', 'PORT': 3306, } } # Password validation # https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] # Internationalization # https://docs.djangoproject.com/en/1.10/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.10/howto/static-files/ STATIC_URL = '/static/' STATICFILES_DIRS =( os.path.join(BASE_DIR,'static'), )
import pymysql pymysql.install_as_MySQLdb()
二.创建数据表
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=62) class Girl(models.Model): """ 女士表 """ nickname = models.CharField(max_length=32) username = models.CharField(max_length=32) password = models.CharField(max_length=62) class B2G(models.Model): """ 关系表 """ b = models.ForeignKey(to ='Boy',to_field='id') g = models.ForeignKey(to='Girl',to_field='id')
三.主内容
from django.shortcuts import render,HttpResponse,redirect # Create your views here. from app01 import models from utils.pager import PageInfo ##############添加数据########### # def test(request): # obj1= [ # models.Boy(nickname='小霸王',username='xbw',password=221), # models.Boy(nickname='小白',username='xb',password=222), # models.Boy(nickname='张晓辉',username='zxh',password=223), # models.Boy(nickname='韩子健',username='hzj',password=224), # models.Boy(nickname='郭伟军',username='gwj',password=225), # models.Boy(nickname='冯伟杰',username='fwj',password=226), # models.Boy(nickname='马克',username='mk',password=227), # models.Boy(nickname='杰西卡',username='jxk',password=228), # models.Boy(nickname='谭胜功',username='tsg',password=229), # models.Boy(nickname='魏超',username='wc',password=220), # ] # models.Boy.objects.bulk_create(obj1,10) # # obj1= [ # models.Girl(nickname='凤姐',username='fj',password=221), # models.Girl(nickname='皮皮鳝',username='pps',password=222), # models.Girl(nickname='皮皮虾',username='ppx',password=223), # models.Girl(nickname='白百何',username='bah',password=224), # models.Girl(nickname='马蓉',username='gmr',password=225), # models.Girl(nickname='卡戴珊',username='kds',password=226), # models.Girl(nickname='波多野结',username='bdyj',password=227), # models.Girl(nickname='苍井空',username='cjk',password=228), # models.Girl(nickname='小雨',username='xy',password=229), # models.Girl(nickname='小雪',username='xx',password=220), # ] # models.Girl.objects.bulk_create(obj1,10) # return HttpResponse('相亲来了么') ####################################### def layout(request): return render(request, 'layout.html') ###############红娘管理################ def hngl(request): return render(request, 'hngl.html') ###############异性列表###################### 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): """ 获取与当前用户有关系的异性 :param request: :return: """ 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') return render(request,'others.html',{'user_list':user_list}) ###################登录界面#################### def login(request): if request.method == 'GET': return render(request,'login.html') else: user = request.POST.get('username') pwd = request.POST.get('password') gender = request.POST.get('gender') rmb = request.POST.get('rmb') # 性别判断 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,'login.html',{'msg': '用户名或密码错误'}) else: # request.session['user_id'] = obj.id # request.session['gender'] = gender # request.session['username'] = user request.session['user_info'] = {'user_id':obj.id,'gender':gender,'username':user,'nickname':obj.nickname} return redirect('/index.html') #################主页面####################### def logout(request): if request.session.get('user_info'): request.session.clear() # request.session.delete(request.session.session_key) return redirect('/login.html') #############男生女生表################### def boy(request): boy_list = models.Boy.objects.all().count() page_info = PageInfo(request.GET.get('page'), boy_list, 10, '/boy.html', 11) us = models.Boy.objects.all()[page_info.start():page_info.end()] return render(request,'boy.html',{'us':us,'page_info':page_info}) def girl(request): girl_list = models.Girl.objects.all().count() page_info = PageInfo(request.GET.get('page'), girl_list, 10, '/girl.html', 11) user_list = models.Girl.objects.all()[page_info.start():page_info.end()] return render(request,'girl.html',{'user_list':user_list,'page_info':page_info}) ###################约会记录表################## def B2G(request): B2G_list = models.B2G.objects.all().count() page_info = PageInfo(request.GET.get('page'), B2G_list, 10, '/B2G.html', 11) user_list = models.B2G.objects.all()[page_info.start():page_info.end()] return render(request,'B2G.html',{'user_list':user_list,'page_info':page_info})
static:
css:
body{ margin: 0; } .left{ float: left; } .right{ float: right; } .hide{ display: none; } .pg-header{ height: 48px; min-width: 1190px; background-color: #5bc0de; line-height: 48px; } .pg-header .logo{ color: white; font-size: 18px; width: 200px; text-align: center; border-right: 1px solid #8a6d3b; } .pg-header .rmenus a{ display: inline-block; padding: 0 19px; color: white; } .pg-header .rmenus a:hover{ background-color: #269abc; } .pg-header .avatar{ padding: 0 20px; } .pg-header .avatar img{ border-radius: 50%; } .pg-header .avatar .user-info{ display: none; background-color: silver; border: 1px solid #dddddd; position: absolute;width: 200px;top: 48px;right: 2px;color: white;z-index: 100; } .pg-header .avatar:hover .user-info{ display: block; } .pg-header .avatar .user-info a{ display: block; } .menus{ width: 200px; position: absolute; left: 0; bottom: 0; top: 48px; border-right: 1px solid #dddddd; background-color:lavender; } .content{ position: absolute; left: 200px; right: 0; top: 48px; bottom: 0; min-width: 990px; overflow: scroll; z-index: 99; } .pg-body .menus a{ display: block; padding: 17px 46px; border-bottom: 1px solid #ffffff; }
images:
plugins:
font-awesome:下载

templates:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css" /> <link rel="stylesheet" href="/static/plugins/font-awesome-4.7.0/css/font-awesome.css" /> <link rel="stylesheet" href="/static/css/commons.css" /> </head> <body> <div class="pg-header"> <div class="logo left">约会记录表</div> <div class="avatar right" style="position: relative"> <img style="width: 40px;height: 40px;" src="/static/images/1.jpg"> <div class="user-info"> <a><i class="fa fa-user" aria-hidden="true"></i>个人资料</a> <a>登陆 </a> <a href="/logout.html">注销</a> <a>退出</a> </div> </div> <div class="rmenus right"> <a>任务<i class="fa fa-snapchat-ghost" aria-hidden="true"></i></a> <a>消息<i class="fa fa-commenting-o" aria-hidden="true"></i></a> <a>通知<i class="fa fa-envelope-o" aria-hidden="true"></i></a> <a>搜索<i class="fa fa-search" aria-hidden="true"></i></a> </div> </div> <div class="pg-body"> <div class="menus"> <a href="/hngl.html"><i class="fa fa-reply" aria-hidden="true"></i>返回首页</a> <a><i class="fa fa-cog" aria-hidden="true"></i>账号设置</a> <a><i class="fa fa-desktop" aria-hidden="true"></i>日志管理</a> </div> <div class="content"> <ol class="breadcrumb"> <li><a href="# ">首页</a></li> </ol> <h4>约会记录表</h4> <div> <table class="table table-bordered table table-hover table table-striped"> <tr> <td>ID</td> <td>男士</td> <td>女士</td> </tr> {% for row in user_list %} <tr> <td>{{ row.id }}</td> <td>{{ row.b_id }}</td> <td>{{ row.g_id}}</td> </tr> {% endfor %} </table> </div> <nav aria-label="Page navigation"> <ul class="pagination"> {{ page_info.pager|safe }} </ul> </nav> </div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css" /> <link rel="stylesheet" href="/static/plugins/font-awesome-4.7.0/css/font-awesome.css" /> <link rel="stylesheet" href="/static/css/commons.css" /> </head> <body> <div class="pg-header"> <div class="logo left">男嘉宾列表</div> <div class="avatar right" style="position: relative"> <img style="width: 40px;height: 40px;" src="/static/images/1.jpg"> <div class="user-info"> <a><i class="fa fa-user" aria-hidden="true"></i>个人资料</a> <a>登陆 </a> <a href="/logout.html">注销</a> <a>退出</a> </div> </div> <div class="rmenus right"> <a>任务<i class="fa fa-snapchat-ghost" aria-hidden="true"></i></a> <a>消息<i class="fa fa-commenting-o" aria-hidden="true"></i></a> <a>通知<i class="fa fa-envelope-o" aria-hidden="true"></i></a> <a>搜索<i class="fa fa-search" aria-hidden="true"></i></a> </div> </div> <div class="pg-body"> <div class="menus"> <a href="/hngl.html"><i class="fa fa-reply" aria-hidden="true"></i>返回首页</a> <a><i class="fa fa-cog" aria-hidden="true"></i>账号设置</a> <a><i class="fa fa-desktop" aria-hidden="true"></i>日志管理</a> </div> <div class="content"> <ol class="breadcrumb"> <li><a href="# ">首页</a></li> </ol> <h4>男嘉宾资料</h4> <div> <table class="table table-bordered table table-hover table table-striped"> <tr> <td>ID</td> <td>姓名</td> <td>用户名</td> <td>密 码</td> </tr> {% for row in us %} <tr> <td>{{ row.id }}</td> <td>{{ row.nickname }}</td> <td>{{ row.username }}</td> <td>{{ row.password }}</td> </tr> {% endfor %} </table> </div> <nav aria-label="Page navigation"> <ul class="pagination"> {{ page_info.pager|safe }} </ul> </nav> </div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css" /> <link rel="stylesheet" href="/static/plugins/font-awesome-4.7.0/css/font-awesome.css" /> <link rel="stylesheet" href="/static/css/commons.css" /> </head> <body> <div class="pg-header"> <div class="logo left">女嘉宾列表</div> <div class="avatar right" style="position: relative"> <img style="width: 40px;height: 40px;" src="/static/images/1.jpg"> <div class="user-info"> <a><i class="fa fa-user" aria-hidden="true"></i>个人资料</a> <a>登陆 </a> <a href="/logout.html">注销</a> <a>退出</a> </div> </div> <div class="rmenus right"> <a>任务<i class="fa fa-snapchat-ghost" aria-hidden="true"></i></a> <a>消息<i class="fa fa-commenting-o" aria-hidden="true"></i></a> <a>通知<i class="fa fa-envelope-o" aria-hidden="true"></i></a> <a>搜索<i class="fa fa-search" aria-hidden="true"></i></a> </div> </div> <div class="pg-body"> <div class="menus"> <a href="/hngl.html"><i class="fa fa-reply" aria-hidden="true"></i>返回首页</a> <a><i class="fa fa-cog" aria-hidden="true"></i>账号设置</a> <a><i class="fa fa-desktop" aria-hidden="true"></i>日志管理</a> </div> <div class="content"> <ol class="breadcrumb"> <li><a href="# ">首页</a></li> </ol> <h4>女嘉宾资料</h4> <div> <table class="table table-bordered table table-hover table table-striped"> <tr> <td>ID</td> <td>姓名</td> <td>用户名</td> <td>密 码</td> </tr> {% for row in user_list %} <tr> <td>{{ row.id }}</td> <td>{{ row.nickname }}</td> <td>{{ row.username }}</td> <td>{{ row.password }}</td> </tr> {% endfor %} </table> </div> <nav aria-label="Page navigation"> <ul class="pagination"> {{ page_info.pager|safe }} </ul> </nav> </div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css" /> <link rel="stylesheet" href="/static/plugins/font-awesome-4.7.0/css/font-awesome.css" /> <link rel="stylesheet" href="/static/css/commons.css" /> </head> <body> <div class="pg-header"> <div class="logo left">女嘉宾列表</div> <div class="avatar right" style="position: relative"> <img style="width: 40px;height: 40px;" src="/static/images/1.jpg"> <div class="user-info"> <a><i class="fa fa-user" aria-hidden="true"></i>个人资料</a> <a>登陆 </a> <a href="/logout.html">注销</a> <a>退出</a> </div> </div> <div class="rmenus right"> <a>任务<i class="fa fa-snapchat-ghost" aria-hidden="true"></i></a> <a>消息<i class="fa fa-commenting-o" aria-hidden="true"></i></a> <a>通知<i class="fa fa-envelope-o" aria-hidden="true"></i></a> <a>搜索<i class="fa fa-search" aria-hidden="true"></i></a> </div> </div> <div class="pg-body"> <div class="menus"> <a href="/hngl.html"><i class="fa fa-reply" aria-hidden="true"></i>返回首页</a> <a><i class="fa fa-cog" aria-hidden="true"></i>账号设置</a> <a><i class="fa fa-desktop" aria-hidden="true"></i>日志管理</a> </div> <div class="content"> <ol class="breadcrumb"> <li><a href="# ">首页</a></li> </ol> <h4>女嘉宾资料</h4> <div> <table class="table table-bordered table table-hover table table-striped"> <tr> <td>ID</td> <td>姓名</td> <td>用户名</td> <td>密 码</td> </tr> {% for row in user_list %} <tr> <td>{{ row.id }}</td> <td>{{ row.nickname }}</td> <td>{{ row.username }}</td> <td>{{ row.password }}</td> </tr> {% endfor %} </table> </div> <nav aria-label="Page navigation"> <ul class="pagination"> {{ page_info.pager|safe }} </ul> </nav> </div> </div> </body> </html>
{% extends 'layout.html' %}
{% block f1 %}
{% include 'user_header.html' %}
<h4>异性列表</h4>
<table class="table table-bordered table table-hover table table-striped">
<tr>
{% for row in user_list %}
<tr>
<td>{{ row.id }}</td>
<td>{{ row.nickname }}</td>
<td>{{ row.username }}</td>
<td>{{ row.password }}</td>
</tr>
{% endfor %}
</tr>
</table>
{% endblock %}
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css" /> <link rel="stylesheet" href="/static/plugins/font-awesome-4.7.0/css/font-awesome.css" /> <link rel="stylesheet" href="/static/css/commons.css" /> {% block boy1 %}{% endblock %} {% block girl1 %}{% endblock %} </head> <body> <div class="pg-header"> <div class="logo left">随缘婚姻网</div> <div class="avatar right" style="position: relative"> <img style="width: 40px;height: 40px;" src="/static/images/1.jpg"> <div class="user-info"> <a><i class="fa fa-user" aria-hidden="true"></i>个人资料</a> <a>登陆 </a> <a href="/logout.html">注销</a> <a>退出</a> </div> </div> <div class="rmenus right"> <a>任务<i class="fa fa-snapchat-ghost" aria-hidden="true"></i></a> <a>消息<i class="fa fa-commenting-o" aria-hidden="true"></i></a> <a>通知<i class="fa fa-envelope-o" aria-hidden="true"></i></a> </div> </div> <div class="pg-body"> <div class="menus"> <a href="/hngl.html"><i class="fa fa-user-circle-o" aria-hidden="true"></i>红娘管理</a> <a><i class="fa fa-address-card" aria-hidden="true"></i>我的资料</a> <a><i class="fa fa-male" aria-hidden="true"></i>单身异性列表</a> <a href="/others.html"><i class="fa fa-venus-double" aria-hidden="true"></i>异性约会记录</a> <a><i class="fa fa-cog" aria-hidden="true"></i>账号设置</a> <a><i class="fa fa-desktop" aria-hidden="true"></i>日志管理</a> </div> <div class="content"> <ol class="breadcrumb"> <li><a href="#">首页</a></li> <li><a href="#">返回</a></li> <li><a href="#">搜索</a></li> </ol> {% block f1 %}{% endblock %} {% block boy %}{% endblock %} {% block girl %}{% endblock %} {% block hngl %}{% endblock %} </div> </div> {% block boy2 %}{% endblock %} {% block girl2 %}{% endblock %} </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form method="POST" action="/login.html"> {% 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> <p> <input type="checkbox" name="rmb" value="11" /> 一个月免登录 </p> <input type="submit" value="提交" />{{ msg }} </form> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css" /> <link rel="stylesheet" href="/static/plugins/font-awesome-4.7.0/css/font-awesome.css" /> <link rel="stylesheet" href="/static/css/commons.css" /> </head> <body> <div class="pg-header"> <div class="logo left">异性约会记录表</div> <div class="avatar right" style="position: relative"> <img style="width: 40px;height: 40px;" src="/static/images/1.jpg"> <div class="user-info"> <a><i class="fa fa-user" aria-hidden="true"></i>个人资料</a> <a>登陆</a> <a href="/logout.html">注销</a> <a >退出</a> </div> </div> <div class="rmenus right"> <a>任务<i class="fa fa-snapchat-ghost" aria-hidden="true"></i></a> <a>消息<i class="fa fa-commenting-o" aria-hidden="true"></i></a> <a>通知<i class="fa fa-envelope-o" aria-hidden="true"></i></a> <a>搜索<i class="fa fa-search" aria-hidden="true"></i></a> </div> </div> <div class="pg-body"> <div class="menus"> <a href="/index.html"><i class="fa fa-reply" aria-hidden="true"></i>返回首页</a> <a><i class="fa fa-cog" aria-hidden="true"></i>账号设置</a> <a><i class="fa fa-desktop" aria-hidden="true"></i>日志管理</a> </div> <div class="content"> <ol class="breadcrumb"> <li><a href="# ">首页</a></li> </ol> <h4>约过会的异性列表</h4> <table class="table table-bordered table table-striped"> {% for row in user_list %} {% if row.g__nickname %} <tr> <th>{{ row.g__nickname }}</th> </tr> {% else %} <tr> <th>{{ row.b__nickname }}</th> </tr> {% endif %} {% endfor %} </table> </div> </div> </body> </html>
<h3>当前用户:{{ request.session.user_info.nickname }}</h3>
三图解
django:


数据库;


浙公网安备 33010602011771号