0
我想了解Python并传递变量。我正在尝试通过一个ID作为'行动'。我可以创建这个动作,看看是否存在,但是我不知道如何把这个id从一种形式传递给另一种形式。如果任何人能指出我的方向是正确的。在Python中传递变量,Flask
Views.py
#g.user.nickname passes in the nickname for a refernce
@app.route('/edit', methods=['GET', 'POST'])
@login_required
def edit():
form = EditForm(g.user.nickname)
if form.validate_on_submit():
g.user.nickname = form.nickname.data
g.user.about_me = form.about_me.data
db.session.add(g.user)
db.session.commit()
flash('Your changes have been saved.')
return redirect(url_for('edit'))
elif request.method != "POST":
form.nickname.data = g.user.nickname
form.about_me.data = g.user.about_me
return render_template('edit.html', form=form)
#Don't understand how to create a variable to pass a reference from post.html
@app.route('/action_request', methods=['GET', 'POST'])
@login_required
def action_requests():
form = EditReqForm()
return render_template('action_request.html',
form=form)
Post.html
<table class="table table-hover">
<tr>
<td>
<p>Raised by <a href="{{ url_for('user', nickname=action.author.nickname) }}">{{ action.raised_by }} </a>on
{{ action.date_raised.strftime('%d-%m-%Y') }}:</p>
#Trying to generate reference of action id to pass to ActionReqForm
<p><a href="{{ url_for('action_requests', id=action.id)}}"><strong>
Issue:</strong>{{
action.issue }}</a></p>
<p><strong>Source: {{action.id}}</strong>{{ action.source }}</p>
<p><strong>Immediate Action: </strong>{{ action.immediate_action }}</p>
</td>
</tr>
</table>
'action_requests'与渲染'形式=形式'在命名空间中。所以如果'id'和'source'是'form'的一部分,可能试试'form ['action']'??我只是猜测,因为我没有看到任何与你发布的代码有关的'action','action.id'或'action.immediate_action'。请参阅:http://flask.pocoo.org/docs/0.10/signals/ – jmunsch 2015-03-31 18:02:25
该函数期望变量列表,因为您只使用'action'它会期望'render_template('action_request.html',action = action) '但由于你只有你的表单,你可以将这些值作为隐藏的输入字段添加到你发布的编辑表单中,并从那里一个接一个地传递它们:'render_template('action_request.html',action_date = form.action_date.data ,action_issue = form.action_issue.data,...等...)' – 2015-03-31 18:07:04
感谢您的建议,我想我可以更好地解释一下。在编辑用户代码中,通过执行以下操作传入数据,即“form.nickname.data = g.user.nickname”。这会用昵称填充表单数据。我想要做的是创建一个类似于g.user的变量,但是对于操作。我希望这是有道理的。 – 2015-04-01 08:24:27