UndefinedError:'user'未定义

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

当我尝试打开users.html时,我收到了上述错误

UndefinedError:'user'未定义

即使我在朋友路线中定义它。

view.朋友

@app.route('/Users', methods=['GET', 'POST'])
def Users():
users = User.query.all()
return render_template('Users.html', contact= users)

@app.route('/friend/<name>')
@login_required
def friend(name):
    user = User.query.filter_by(name = name).first()
    if user is None:
        flash('User %s not found.' % name)
        return redirect(url_for('index'))
    if user == g.user:
        flash('You can\'t Friend yourself!')
        return redirect(url_for('Users', name = name, user=user))
    u = g.user.be_friend(user)
    if u is None:
        flash('Cannot Friend ' + name + '.')
        return redirect(url_for('Users', name = name, user=user))
    db.session.add(u)
    db.session.commit()
    flash('You are now Friend with' + name + '!')
    return redirect(url_for('index', name=name, user=user))

User.html

{% extends "base.html" %}
   {% block content %}
     <h2>show all users</h2>
         {% for contact in contact  %}
            <strong>name:</strong><a href={{url_for('profile', id=contact.id)}}> 
           {{  contact.name}}</a><br>
           # Got error here eventhough I define user in friend route
           <a href="{{ url_for('friend', name= user.name) }}">Friend</a>
         {% endfor %}
       {% endblock %}
flask flask-sqlalchemy jinja2
1个回答
0
投票

user.html中,你称之为变量contact。你可能想把它叫做user

{% for user in contact %}

您还需要更新对contact.idcontact.name的引用。

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