温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

一个小的登录查看的session案例

发布时间:2020-07-15 03:49:04 来源:网络 阅读:2641 作者:kesungang 栏目:web开发

需求

1,创建两个页面,一个是登录页面,一个是登录成功后,显示个人信息的页面
2,登录页面是基于session
3、数据表(男生,女生,男女关系)
4、登录页(用户名,密码,性别,一周免登陆)
5、内容页,异性列表和有关系的列表

设计

views的改变

把登陆的代码和关系内容的代码分开,所以创建了views目录,并且把不同的内容写到了不同的模块中。如图
一个小的登录查看的session案例
各个py文件代码

from django.shortcuts import render,HttpResponse,redirect
from app_01 import models
def acc_login(request):
    if request.method == "GET":
        return render(request,'acclogin.html')
    else:
        user = request.POST.get('username')
        gender = request.POST.get('gender')
        pwd = request.POST.get('password')
        rmb = request.POST.get('rmb')
        print(user,gender,pwd,rmb)
        if gender == "1":
            obj = models.AccBoy.objects.filter(username=user,password=pwd).first()

        else:
            obj = models.AccGirl.objects.filter(username=user,password=pwd).first()
        if not obj:
            return render(request,'acclogin.html',{'msg':'用户名或密码错误'})
        else:
            request.session['user_info'] = {'user_d':obj.id,'gender':gender,'rmb':rmb,'user':user,'nickname':obj.nickname}
            return redirect('/accindex.html')

def acclogout(request):
    if request.session.get('user_info'):
        request.session.clear()
        return redirect('/acclogin.html')
from django.shortcuts import render,redirect,HttpResponse
from app_01 import models

def accindex(requtst):
    if not requtst.session.get('user_info'):
        return redirect('/acclogin.html')
    else:
        gender = requtst.session.get("user_info").get('gender')
        if gender == '1':
            user_list = models.AccGirl.objects.all()
        else:
            user_list = models.AccBoy.objects.all()
        return render(requtst,'accindex.html',{'user_list':user_list})
html代码

accindex

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>accindex</title>
</head>
<body>
    <h3>当前用户:{{ request.session.user_info.nickname }}</h3>
    <p><a href="/acclogin.html">注销用户</a></p>
    <h3>用户列表</h3>
    <hr>
    <ul>
        {% for item in user_list %}
            <li>{{ item.username }}</li>
        {% endfor %}
    </ul>
</body>
</html>

acclogin代码

<form action="/acclogin.html" method="post">
    {% csrf_token %}
    <p><input type="text" name="username">用户名</p>
    <p><input type="password" name="password">密码</p>
    <input type="radio" name="gender" value="1">男
    <input type="radio" name="gender" value="2">女
    <input type="checkbox" value="11" name="rmb" />一周内免登陆
    <p><input type="submit" value="提交"></p>{{ msg }}
</form>

related_name知识点

一个小的登录查看的session案例
一个小的登录查看的session案例

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=64)
    gender_choices = (
        (1,'男'),
        (2,'女'),
    )
    gender = models.IntegerField(choices=gender_choices)

    m = models.ManyToManyField('UserInfo')

"""
1  女
2  女
3  女
4  男
5  男
a1 = 2
a2 = 5
"""
# related_query_name
# obj对象男.b_set.all()
# obj对象女.a_set.all()
# related_name
# obj对象男.a.all()
# obj对象女.b.all()
# class U2U(models.Model):
#     g = models.ForeignKey('UserInfo',related_name='boys')
#     b = models.ForeignKey('UserInfo',related_name='girls')

# U2U.objects.create(g_id=a1,b_id=a2)
# related_query_name,related_name

# class UserType(models.Model):
#     title = models.CharField(max_length=32)
#
# class User(models.Model):
#     username = models.CharField(max_length=32)
#     ut = models.ForeignKey('UserType',related_query_name='xxxx')

# 反向:
# related_name='xxxx'
#   user_set ==> xxxx
# related_query_name='xxxx'
#   user_set ==> xxxx_set

# FK自关联

class Comment(models.Model):
    """
    评论表
    """
    news_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='xxxx')
"""
   新闻ID                         reply_id
1   1        别比比    root         null
2   1        就比比    root         null
3   1        瞎比比    shaowei      null
4   2        写的正好  root         null
5   1        拉倒吧    由清滨         2
6   1        拉倒吧1    xxxxx         2
7   1        拉倒吧2    xxxxx         5
"""
"""
新闻1
    别比比
    就比比
        - 拉倒吧
            - 拉倒吧2
        - 拉倒吧1
    瞎比比
新闻2:
    写的正好
"""
# def test(request):
#     # models.U2U.objects.create(b_id=2,g_id=6)
#     # models.U2U.objects.create(b_id=1,g_id=6)
#     # models.U2U.objects.create(b_id=1,g_id=4)
#     # models.U2U.objects.create(b_id=1,g_id=5)
#
#     # boy = models.UserInfo.objects.filter(gender=1,id=2).first()
#     # girl = models.UserInfo.objects.filter(gender=2,id=6).first()
#     # models.U2U.objects.create(b=boy,g=girl)
#
#     # UserInfo对象
#     # xz = models.UserInfo.objects.filter(id=1).first()
#     # # 和徐峥有关系的所有信息:U2U列表[U2U对象,2U对象,2U对象,]
#     # result = xz.girls.all()
#     # for u in result:
#     #     # U2U对象
#     #     print(u.g.nickname)
#
#     # 查男生
#     # xz = models.UserInfo.objects.filter(id=1).first()
#     # u = xz.m.all()
#     # for row in u:
#     #     print(row.nickname)
#     # 查女神
#     # xz = models.UserInfo.objects.filter(id=4).first()
#     # v = xz.userinfo_set.all()
#     # for row in v:
#     #     print(row.nickname)
#     return HttpResponse('...')
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI