基于session认证  相亲小作业

 用户登录  如果男用户登录,显示女生列表
如果女用户登录,显示男生列表

urls

===========================urls===========================================================

"""s4day74 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
""" from django.conf.urls import url
from django.contrib import admin
from app01.views1 import love
from app01.views1 import account
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'^test.html$',views.test), # url(r'^login/', views.login), # url(r'^index/', views.index), # url(r'^test.html$', love.test), url(r'^login.html$', account.login),
url(r'^index.html$', love.index),
url(r'^logout.html$', account.logout),
url(r'^others.html$', love.others), ]

models

=============================models===============================================
from django.db import models # Create your models here. # class UserInfo(models.Model): 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")
g = models.ForeignKey(to="Girl",to_field="id")

account.py

=============================account.py===============================================
from django.shortcuts import render,HttpResponse,redirect
from app01 import models 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:
#session里面设置值,可以嵌套 相当于归类 一个key对应一条条信息 # request.session['user_id']=obj.id # request.session["gender"]=gender # request.session["username"]=user if rmb:
request.session.set_expiry(15)
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()
return redirect('/login.html')

love.py

============================= love.py===============================================
from django.shortcuts import render,HttpResponse,redirect
from app01 import models
from utils.pager import PageInfo def index(request):
if not request.session.get("user_info"):
return redirect("/login.html")
else:
#到session里面获取性别
gender=request.session.get("user_info").get('gender')
if gender == "1":
# user_list=models.Girl.objects.all()
all_count = models.Girl.objects.all().count()
page_info = PageInfo(request.GET.get('page'), all_count, 10, '/boy.html', 11)
user_list = models.Girl.objects.all()[page_info.start():page_info.end()]
return render(request, 'index.html', {'user_list': user_list, 'page_info': page_info})
else:
# user_list=models.Boy.objects.all()
all_count = models.Boy.objects.all().count()
page_info = PageInfo(request.GET.get('page'), all_count, 10, '/boy.html', 11)
user_list = models.Boy.objects.all()[page_info.start():page_info.end()]
return render(request, 'index.html', {'user_list': user_list, 'page_info': page_info}) # 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})

login.html

============================= login.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

=============================  index.html===================================================================================================================
!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css"> </head> <body>
{# <h2>当前用户:{{ request.session.user_info.nickname }}</h2>#} {# <a href="/logout.html">注销</a>#} {% include 'user_header.html' %}
<h3>异性列表</h3> <a href="/others.html">查看和我有关的异形</a> <table class="table table-striped table-bordered table table-hover table table-condensed"> <tr> <th>ID</th> <th>姓名</th> <th>密码</th> </tr>
{% for row in user_list %}
<tr> <td>{{ row.id }}</td> <td>{{ row.nickname }}</td> <td>{{ row.password }}</td> </tr>
{% endfor %} </table> <nav aria-label="Page navigation"> <ul class="pagination">
{{ page_info.pager|safe }}
</ul> </nav> </body> </html>

others.html

============================= 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>

user_header.html

<h2>当前用户:{{ request.session.user_info.nickname }}</h2> <a href="/logout.html">注销</a>

最新文章

  1. java IO流 之 其他流
  2. Linux 文件访问权限
  3. linux学习 命令ll后字段的解释(转)
  4. Spring day02笔记
  5. vijos1053 用spfa判断是否存在负环
  6. SignalR来做实时Web聊天
  7. Servlet第七篇【Cookie和Session的区别、应用】
  8. MySQL性能建议者mysqltuner.pl和pt-variable-advisor
  9. 谈谈对Python的感想
  10. V4L2驱动的移植与应用(二)
  11. win10+tensorflow+CUDA 心酸采坑之路
  12. 对B+树,B树,红黑树的理解
  13. Polish Extraction Zone
  14. Code First 重复外键
  15. Package has no installation candidate解决方法
  16. MySQL客户端管理
  17. c++ 数组操作(转)
  18. mysql 下载
  19. Android Runtime.getRuntime().exec
  20. BigData:值得了解的十大数据发展趋势

热门文章

  1. nodejs中的require,exports使用说明
  2. Maven-11: 从命令行调用插件
  3. Webpack 引入bootstrap
  4. 【Linux】 CentOS7 虚拟机配置
  5. sql操作知识点个人笔记(SQLServer篇)
  6. Linux中SVN的备份与恢复
  7. lua向文件中写入数据,进行记录
  8. JavaScript(第三天)【数据类型】
  9. 团队项目7——团队冲刺(beta版本)
  10. 轻量级django 一