You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django如何实现用户专属页面展示登录用户请假状态

实现方案

核心逻辑基于Django自带的认证系统实现:请求对象里的request.user会自动携带当前登录的用户信息,直接用这个字段过滤LeaveApp表的关联数据即可,全程不需要前端传递用户标识,同时搭配登录校验装饰器拦截未登录访问。

分步实现
  • 编写视图函数
    首先导入登录校验装饰器和对应的模型,在视图层直接完成数据过滤,不要把全量数据传到前端再筛选,避免数据泄露和性能浪费。

    # views.py
    from django.shortcuts import render
    from django.contrib.auth.decorators import login_required
    from .models import LeaveApp
    
    # 替换login_url为你项目实际的登录页路由
    @login_required(login_url='/login/')
    def my_leave_records(request):
        # 筛选当前用户的所有请假记录,按请假开始日期倒序排列
        user_leaves = LeaveApp.objects.filter(authuser=request.user).order_by('-fdate')
        context = {
            'user_leaves': user_leaves
        }
        return render(request, 'my_leave.html', context)
    
  • 配置路由
    在对应app的urls.py中添加页面路由:

    # urls.py
    from django.urls import path
    from . import views
    
    urlpatterns = [
        path('my/leaves/', views.my_leave_records, name='my_leave_records'),
    ]
    
  • 编写前端模板
    在templates目录下新建my_leave.html,遍历传入的请假记录数据集渲染即可,Django模板可以直接拿到request.user对象展示当前用户信息:

    <!-- my_leave.html -->
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <title>我的请假</title>
        <style>table{border-collapse: collapse;width: 80%;margin: 20px auto;}th,td{border:1px solid #ccc;padding:8px;text-align: center;}</style>
    </head>
    <body>
        <h2 style="text-align: center;">{{ request.user.username }} 的请假记录</h2>
        <table>
            <thead>
                <tr>
                    <th>请假类型</th>
                    <th>开始日期</th>
                    <th>结束日期</th>
                    <th>请假原因</th>
                </tr>
            </thead>
            <tbody>
                {% for leave in user_leaves %}
                <tr>
                    <td>{{ leave.get_type_of_leave_display }}</td>
                    <td>{{ leave.fdate|date:"Y-m-d" }}</td>
                    <td>{{ leave.tdate|date:"Y-m-d" }}</td>
                    <td>{{ leave.reason }}</td>
                </tr>
                {% empty %}
                <tr>
                    <td colspan="4">暂无请假记录</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </body>
    </html>
    
补充说明
  • 你当前的LeaveApp模型缺少审批状态字段,如果需要展示请假的审批进度,可以给模型新增状态字段:
    # 在LeaveApp类中添加
    status_choices = (
        ('pending', '待审批'),
        ('approved', '已通过'),
        ('rejected', '已驳回'),
    )
    status = models.CharField(max_length=20, choices=status_choices, default='pending')
    
    新增字段后需要执行python manage.py makemigrations和python manage.py migrate同步数据库结构,之后就可以在模板中渲染leave.get_status_display展示审批状态。
  • 必须使用request.user作为数据筛选的唯一用户依据,不要通过URL参数、表单提交的用户ID查询数据,避免水平越权漏洞,防止用户篡改参数查看其他人的请假信息。
  • @login_required装饰器必须添加,未登录用户访问该地址时会自动跳转到你配置的登录页,避免匿名用户访问报错。

内容的提问来源于stack exchange,提问作者Divyom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.01 12:21:34