【发布时间】:2016-10-11 18:23:31
【问题描述】:
这更多的是关于在哪里可以找到我将如何做到这一点的问题,而不是询问如何做到这一点。我确信这已经很好地涵盖了,我只是在努力阐明我可以在 Google 中使用的正确术语并找出答案。
无论如何 - 我有一个 Python Flask Web 应用程序。在网页上,有一个请求用户输入的输入框。我想要发生的事情是在后台用户输入发生一些魔术。在我自己的场景中,我想获取一个 URL,然后使用 bs4 选择我需要的内容并将其显示在网页上。
为了简单起见,我会要求一些简单的东西,然后我可以从那里开始构建:如果我要求用户指定一个数字然后按“提交”,我怎么能将这个数字乘以 10?
如果我的表单代码是index.html:
<form class="form-horizontal" role="form" method="post" action="/">
{{ form.csrf_token }}
<fieldset>
<div class="form-group">
<label for="GetNum" class="col-lg-2 control-label">Enter Number</label>
<div class="col-lg-6">
<input type="text" id="GetNum" name="GetNum" class="form-control" value="">
</div>
<input class="btn btn-success" type="submit" value="Calculate">
</div>
</fieldset>
</form>
我注意到我可以通过<p>form.request.GetNum</p> 将输入打印到段落中。
现在对于这个问题的示例,后端功能的代码将是:
import math
GetNum = 10 #should be form.request.GetNum value
CalcResult = GetNum*1000
print CalcResult # or {{ CalcResult.data }} or something in index.html
我的控制器 (app.py) 看起来像:
@app.route('/', methods=['GET', 'POST'])
@login_required
def home():
error = None
form = PostForm(request.form) # unrelated to question, but will this clash?
if .. :
do something
return redirect(..)
else:
do something else..
return render_template(..)
我担心如果我必须将数学函数放在那里,home 函数最终会产生大量代码。所以 tl; 博士,我将如何在我的代码中实现后端功能? (或者请给我提供阅读材料,谢谢!)
另一件事是我的“/”页面上已经有一个表单,我是否必须将表单重命名为 form1 form2,因为它们会发生冲突还是可以?
【问题讨论】: