博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Django 基于session认证 小作业
阅读量:5024 次
发布时间:2019-06-12

本文共 5998 字,大约阅读时间需要 19 分钟。

 基于session认证  相亲小作业

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

  

"""s4day74 URL ConfigurationThe `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 urlfrom django.contrib import adminfrom app01.views1 import lovefrom app01.views1 import accounturlpatterns = [    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),]
urls
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")
models
from django.shortcuts import render,HttpResponse,redirectfrom app01 import modelsdef 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')
account.py
from django.shortcuts import render,HttpResponse,redirectfrom app01 import modelsfrom utils.pager import PageInfodef 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})
love.py
    
Title {
% include 'user_header.html' %}

有关系的异性列表

    {
    % for row in user_list%} {
    % if row.g__nickname %}
  • {
    { row.g__nickname }}
  • {
    % else %}
  • {
    { row.b__nickname }}
  • {
    % endif %} {
    % endfor %}
login.html
!DOCTYPE html>    
Title
{
#

当前用户:{
{ request.session.user_info.nickname }}

#}{
# 注销#} {% include 'user_header.html' %}

异性列表

查看和我有关的异形
{
% for row in user_list %}
{
% endfor %}
ID 姓名 密码
{
{ row.id }}
{
{ row.nickname }}
{
{ row.password }}
index.html
    
Title {
% include 'user_header.html' %}

有关系的异性列表

    {
    % for row in user_list%} {
    % if row.g__nickname %}
  • {
    { row.g__nickname }}
  • {
    % else %}
  • {
    { row.b__nickname }}
  • {
    % endif %} {
    % endfor %}
others.html

当前用户:{
{ request.session.user_info.nickname }}

注销
user_header.html

 

转载于:https://www.cnblogs.com/niejinmei/p/7123272.html

你可能感兴趣的文章
【转】JAVA字符串格式化-String.format()的使用
查看>>
【转】ButterKnife基本使用--不错
查看>>
【转】VS2012编译出来的程序,在XP上运行,出现“.exe 不是有效的 win32 应用程序” “not a valid win32 application”...
查看>>
函数中关于const关键字使用的注意事项
查看>>
微信架构(转)
查看>>
Web项目中的路径问题
查看>>
js随机数的取整
查看>>
关于解析漏洞
查看>>
十大经典预测算法(六)---集成学习(模型融合算法)
查看>>
用php做一个简单的注册用户功能
查看>>
一款基于css3的3D图片翻页切换特效
查看>>
Feign使用Hystrix无效原因及解决方法
查看>>
Sizeof与Strlen的区别与联系
查看>>
hadoop2.2.0_hbase0.96_zookeeper3.4.5全分布式安装文档下载
查看>>
Flutter 贝塞尔曲线切割
查看>>
golang 的编译安装以及supervisord部署
查看>>
easyui源码翻译1.32--Dialog(对话框窗口)
查看>>
阿里架构师,讲述基于微服务的软件架构模式
查看>>
Eclipse导入maven项目时,Pom.xml文件报错处理方法
查看>>
01、JAVA开发准备
查看>>