Flask 操作未重定向到指定的 html 页面

问题描述 投票:0回答:1

我正在pycharm中使用flask创建一个简单的网站。我正在开发允许用户注册网站帐户并使用他们创建的凭据登录的功能。我目前的问题是注册表单并没有始终重定向到登录页面。仅在提供用户名和合适的密码后刷新页面。

这是我目前尝试过的。

Registration HTML Form:

<form method="POST" action="{{url_for('register')}}">
        <!-- Defines username Fields -->
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" required><br><br>
        <!-- Defines password Fields -->
        <label for="password">Password:</label>
        <input type="password" id="password" name="password" required><br><br>
        <!-- Defines Register Button -->
        <button type="submit">Register</button>
    </form>
Login HTML Form:

<form method="POST" action="{{url_for('login')}}">
        <!-- Defines username Fields -->
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" required><br><br>
        <!-- Defines password Fields -->
        <label for="password">Password:</label>
        <input type="password" id="password" name="password" required><br><br>
        <!-- Defines Login Button -->
        <button type="submit">Login</button>
    </form>
Web Page Routes for both login and registration blocks:
from datetime import date
import re
from flask import Flask, render_template,  request, redirect, url_for, session

# Calls the imported flask function and sets to render the web pages in the "template" form
app = Flask(__name__, template_folder='template')
app.secret_key = 'your_secret_key'

#User Data and variables being used
users = [{'username': 'password'}]
#Password Complexity
PASSWORD_REGEX = r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,}$'

# Defining the website routes
@app.route('/')
# Sets up registration page for the site
@app.route('/register', methods=['GET', 'POST'])
def register():
    '''Function that renders the registration page'''
    if request.method == 'POST':
        # Establishes the username and password variables to be inputted into the form
        username = request.form['username']
        password = request.form['password']
        if username in users:
            # Refreshes the page and prints a message when username already exists.
            return render_template('register.html', message='Username already exists.')
        if not re.match(PASSWORD_REGEX, password):
            # Refreshes the page and prints a message when password isn't complex enough
            return render_template('register.html', message='Password complexity not met.')

        users[username] = password
        # Redirects to the login screen
        return redirect(url_for('login'))

    return render_template('register.html',date=date.today())

# Sets up login page for the site
@app.route('/login', methods=['GET', 'POST'])
def login():
    '''Function that renders the login page'''
    if request.method == 'POST':
        # Establishes the username and password variables to be inputted into the form
        username = request.form['username']
        password = request.form['password']

        if username in users and users[username] == password:
            session['username'] = username
            # Redirects to the home page screen
            return redirect(url_for('home_page'))
        # Refreshes the page and prints a message when username already exists.
        return render_template('login.html', message='Invalid credentials.')
    #Renders the login page
    return render_template('login.html',date=date.today())
python html forms flask
1个回答
0
投票

没有重定向到登录页面并停留在注册页面的事实意味着它正在通过以下情况之一:

if username in users:
        # Refreshes the page and prints a message when username already exists.
        return render_template('register.html', message='Username already exists.')
if not re.match(PASSWORD_REGEX, password):
        # Refreshes the page and prints a message when password isn't complex enough
        return render_template('register.html', message='Password complexity not met.')

它使用消息渲染寄存器模板,但问题是您没有在寄存器模板中显示渲染的消息,以便它向您显示它。

解决方案:假设您使用的是 jinja 模板中内置的 Flask,您可以通过这种方式显示消息,请注意,我添加了一些样式以使其在浏览器中清晰可见:

<form method="POST" action="{{url_for('register')}}">
    {% if message %}
        <div style="margin: 1em 0;padding: 1em;background: #cae6f6;border: 1px solid #377ba8;">   {{message}} </div> <br><br>
    {% endif %}
    <!-- Defines username Fields -->
    <label for="username">Username:</label>
    <input type="text" id="username" name="username" required><br><br>
    <!-- Defines password Fields -->
    <label for="password">Password:</label>
    <input type="password" id="password" name="password" required><br><br>
    <!-- Defines Register Button -->
    <button type="submit">Register</button>
</form>

© www.soinside.com 2019 - 2024. All rights reserved.