href 中的 flask url_for 未重定向到正确的 flask 方法并呈现错误的页面

flask url_for in href is not redirecting to the correct flask method and rendering wrong page

我正在构建一个 Flask 应用程序。我有两个 methods/routes 做两件不同的事情。一条路线重定向到 profile.html,第二条路线应该重定向到 update_profile.html

我的问题是在我的配置文件 html 文件中,有一个编辑配置文件 link 使用 url_for('views.profile_update') 重定向到 profile_update route/method .当我单击 link 时,它只呈现作为配置文件方法一部分的相同配置文件页面,而不是 profile_update 方法。不确定为什么会这样...

views.py

from flask import Blueprint, render_template, session, redirect, url_for, request
from . import db

views = Blueprint('views', __name__)

@views.route('/profile', methods=(['GET']))
def profile():
  if 'username' in session:
    print('profile page')
    print(session['username'])
    loggedInUser = db.users.find_one({'username': session['username']})
    return render_template('profile.html', loggedInUser=loggedInUser)
  else:
    return redirect(url_for('auth.login'))

@views.route('/profile', methods=['GET', 'POST'])
def profile_update():
  if 'username' in session:
    print('profile update')
    return render_template('update_profile.html')
  else:
    return redirect(url_for('auth.login'))

profile.html

{% extends 'base.html' %}

{% block title %} PROFILE {% endblock %}

{% block navbar %}
  <a class="nav-item nav-link" id="feed" href="/">Feed</a>
  <a class="nav-item nav-link" id="activity" href="/">Activity</a>
  <a class="nav-item nav-link" id="photos" href="/">Photos</a>
  <a class="nav-item nav-link" id="edits" href="/">Edits</a>
  <a class="nav-item nav-link" id="profile" href="/profile">Profile</a>
  <a class="nav-item nav-link" id="logout" href="/auth/logout">Logout</a>
{% endblock %}

{% block content %}

<p>{{ loggedInUser.profile_pic }}</p>
<h1>{{ loggedInUser.username }}</h1>
<p>{{ loggedInUser.following }}</p>
<p>{{ loggedInUser.followers }}</p>
<p>{{ loggedInUser.email }}</p>
<p>{{ loggedInUser.name }}</p>
<p>{{ loggedInUser.location }}</p>
<p>{{ loggedInUser.bio }}</p>
<p>{{ loggedInUser.photo_list }}</p>
<p>{{ loggedInUser.edit_list }}</p>

<a class="nav-item nav-link" id="update_profile" href={{ url_for('views.profile_update') }}>Edit Profile</a>

{% endblock %}

更新_profile.html

{% extends 'base.html' %}

{% block title %} UPDATE PROFILE {% endblock %}

{% block navbar %}
  <a class="nav-item nav-link" id="feed" href="/">Feed</a>
  <a class="nav-item nav-link" id="activity" href="/">Activity</a>
  <a class="nav-item nav-link" id="photos" href="/">Photos</a>
  <a class="nav-item nav-link" id="edits" href="/">Edits</a>
  <a class="nav-item nav-link" id="profile" href="/profile">Profile</a>
  <a class="nav-item nav-link" id="logout" href="/auth/logout">Logout</a>
{% endblock %}

{% block content %}

<form actions="/auth/signup" method="POST">
  <header>Sign Up</header>
  <div class="form-group">
    <label for="username">Username</label>
    <input type="text" id="username" name="username" defaultValue="Enter Username"/>
    <label for="password">Password</label>
    <input type="password" for="password" name="password" defaultValue="Enter Password"/>
    <label for="password-confirm">Confirm Password</label>
    <input type="password" for="password-confirm" name="password-confirm" defaultValue="Confirm Password"/>
    <label for="email">Email</label>
    <input type="email" for="email" name="email" defaultValue="Enter Email"/>
    <label for="name">Name</label>
    <input type="text" for="name" name="name" defaultValue="Enter Name"/>
    <label for="account">Account</label>
    <select name="account">
      <option value="photographer">Photographer</option>
      <option value="videographer">Videographer</option>
      <option value="editor">Editr</option>
      <option value="designer">Designer</option>
    </select>
    <label for="bio">Bio</label>
    <input type="text" for="bio" name="bio" defaultValue="Enter Bio"/>
    <label for="location">Location</label>
    <input type="text" for="location" name="location" defaultValue="Enter City, State"/>
    <input type="submit" name="submit" value="Sign Up"/>
  </div>
</form>

{% endblock %}

应该发生的是,当您获得 profile.html 文件并单击编辑配置文件 link 时,它应该重定向到 profile_update,然后呈现update_profile.html 文件。现在,它只是继续渲染 profile.html 文件。当我将它重定向到主页之类的东西时,它可以正常工作。 profile_update 是唯一不起作用的 method/route。

问题是您对两条路线使用相同的url:

@views.route('/profile', methods=(['GET']))
def profile():
    ...

@views.route('/profile', methods=['GET', 'POST'])
def profile_update():
    ...

您可以通过将 profile_update url 更改为如下内容来解决此问题:

@views.route('/profile/update', methods=['GET', 'POST'])
def profile_update():
    ...