我正在我的网站上实现一个管理页面,但即使我输入了正确的凭据,它仍然说"没有访问"

app.py:

@app.route('/login')
def login():
    return render_template('login.html', incorrect=False)

@app.route('/login', methods=['POST'])
def login_post():
    creds = dict(request.form)
    if creds['username'] == USERNAME and creds['password'] == PASSWORD:
        return redirect(url_for('admin', authenticated=1))
    else:
        return render_template('login.html', incorrect=True)

@app.route('/admin')
def admin(authenticated=0):
    conn = sqlite3.connect('articles.db')
    cur = conn.cursor()
    cur.execute("SELECT * FROM PENDING")
    articles = cur.fetchall()
    #print(articles)
    conn.close()
    return render_template('admin.html', authenticated=authenticated, articles=articles)

注意:凭证都是admin(设置在. inf文件中)

Login.html:

{% extends "base.html" %}

{% block title %}Login{% endblock %}
{% block content %}
<div class="write container py-3 mx-auto bg-muted text-white" style="width: 60vw;">
    <h1 action="{{ url_for('write_post') }}" data-aos="fade-up">Admin Login</h1>
    {% if incorrect %}
        <div class="alert alert-danger alert-dismissible">
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            <strong>Incorrect Credentials</strong> Please try again.
        </div>
    {% endif %}
    <form method="POST" enctype=multipart/form-data>
        <label data-aos="fade-up" data-aos-delay="50" for="username">Username:</label><br>
            <input data-aos="fade-up" data-aos-delay="100" required maxlength="255" id="username" name="username"><br><br>
        <label data-aos="fade-up" data-aos-delay="150" for="password">Password:</label><br>
            <input data-aos="fade-up" data-aos-delay="200" required maxlength="255" id="password" name="password"><br><br>
        <input data-aos="fade-up" data-aos-delay="450" class="btn btn-success" type="submit">
    </form>
</div>
{% endblock %}

admin.html:

{% extends "base.html" %}

{% block title %}Admin{% endblock %}

{% block content %}
{% if authenticated %}
<div data-aos="fade-down" class=".container p-5 bg-muted text-white">
    <h1>Articles pending review: </h1>
</div>
{% else %}
<div data-aos="fade-down" class=".container p-5 bg-muted text-white">
    <h1>No Access</h1>
    <p>You are not logged in!</p>
</div>
{% endif %}
{% endblock %}

在www.example.com中,即使我通过了1,admin.html中的if authenticated计算为false. 网址是http://127.0.0.1:5001/admin?authenticated=1 flask服务器运行时使用flask run --debug --port 5001 我该如何解决这个问题,并显示管理页面的内容,如果登录?

推荐答案

您正在将已验证的参数传递给管理路由.当您从login_post函数重定向时,您没有正确传递authenticated参数.

在login_post函数中,当成功登录后重定向到admin路由时,需要将authenticated参数作为URL查询字符串的一部分传递.以下是你可以做到的:

from flask import request, redirect, url_for

@app.route('/login', methods=['POST'])
def login_post():
    creds = dict(request.form)
    if creds['username'] == USERNAME and creds['password'] == PASSWORD:
        return redirect(url_for('admin', authenticated=1))  # Passing authenticated=1
    else:
        return render_template('login.html', incorrect=True)

这样,当登录成功时,它将重定向到管理页面,其中authenticated参数设置为1.

此外,似乎您在管理路由中使用了默认参数进行身份验证.这不是必需的,因为您已经显式地传递了它.您可以修改您的管理员路由:

@app.route('/admin')
def admin():
    authenticated = request.args.get('authenticated', default=0, type=int)
    if authenticated:
        conn = sqlite3.connect('articles.db')
        cur = conn.cursor()
        cur.execute("SELECT * FROM PENDING")
        articles = cur.fetchall()
        conn.close()
        return render_template('admin.html', authenticated=authenticated, articles=articles)
    else:
        return render_template('admin.html', authenticated=authenticated)

这应该可以.

Python相关问答推荐

如何使用matplotlib在Python中使用规范化数据和原始t测试值创建组合热图?

Python虚拟环境的轻量级使用

用砂箱开发Web统计分析

我的字符串搜索算法的平均时间复杂度和最坏时间复杂度是多少?

为什么常规操作不以其就地对应操作为基础?

剪切间隔以添加特定日期

为什么'if x is None:pass'比'x is None'单独使用更快?

具有相同图例 colored颜色 和标签的堆叠子图

如何在海上配对图中使某些标记周围的黑色边框

以异步方式填充Pandas 数据帧

使用嵌套对象字段的Qdrant过滤

使用python playwright从 Select 子菜单中 Select 值

如何在Python中自动创建数字文件夹和正在进行的文件夹?

浏览超过10k页获取数据,解析:欧洲搜索服务:从欧盟站点收集机会的微小刮刀&

删除Dataframe中的第一个空白行并重新索引列

查找数据帧的给定列中是否存在特定值

两个名称相同但值不同的 Select 都会产生相同的值(discord.py)

Django查询集-排除True值

牛郎星直方图中分类列的设置顺序

搜索结果未显示.我的URL选项卡显示:http://127.0.0.1:8000/search?";,而不是这个:";http://127.0.0.1:8000/search?q=name";