当我尝试打开users.html时,我收到了上述错误
UndefinedError:'user'未定义
即使我在朋友路线中定义它。
view.py
@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 %}
答案 0 :(得分:0)
在user.html
中,您调用变量contact
。您可能想要将其称为user
。
{% for user in contact %}
您还需要更新对contact.id
和contact.name
的引用。