简体   繁体   English

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

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

I have a flask application I am building.我正在构建一个 flask 应用程序。 I have two methods/routes that do two different things.我有两种方法/路线可以做两种不同的事情。 one route redirects to profile.html and the second route is supposed to redirect to update_profile.html一条路由重定向到profile.html ,第二条路由应该重定向到update_profile.html

my issues is that in my profile html file, there is an edit profile link that uses url_for('views.profile_update') to redirect to the profile_update route/method.我的问题是,在我的配置文件 html 文件中,有一个编辑配置文件链接使用url_for('views.profile_update')重定向到 profile_update 路由/方法。 when I click the link, it just renders the same profile page that is part of the profile method rather than profile_update method.当我单击该链接时,它只会呈现属于 profile 方法而不是 profile_update 方法的同一个人资料页面。 Not sure why this is happening...不知道为什么会这样……

views.py视图.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配置文件.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 %}

update_profile.html update_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 %}

What is supposed to happen is that when you get the profile.html file and click on the edit profile link, it is supposed to redirect to the profile_update and then render the update_profile.html file.应该发生的是,当您获取 profile.html 文件并单击编辑配置文件链接时,它应该重定向到profile_update然后呈现 update_profile.html 文件。 Right now, it just keeps rendering the profile.html file.现在,它只是不断渲染 profile.html 文件。 When i redirect it to something like home page, it works correctly.当我将它重定向到主页之类的东西时,它可以正常工作。 The profile_update is the only method/route that is not working. profile_update 是唯一不起作用的方法/路线。

The issue is that you are using the same url for both routes:问题是您对两条路线都使用相同的 url :

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

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

You could fix this by changing the profile_update url to something like this:您可以通过将profile_update url 更改为以下内容来解决此问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM