[英]Python Flask - Processing input obtained from front-end in the back-end
this is more of a question about where to find how I would do this rather than asking how to do this.这更多是关于在哪里可以找到我将如何做到这一点的问题,而不是询问如何做到这一点。 I'm sure this is well covered, I'm just struggling to articulate the correct term which I could use to Google and find out the answer.
我确信这已经涵盖得很好,我只是在努力阐明我可以用来谷歌并找出答案的正确术语。
Anyway - I have a Python Flask web application.无论如何 - 我有一个 Python Flask Web 应用程序。 On a web page, there is an input box which requests user input.
在网页上,有一个请求用户输入的输入框。 What I would like to happen, is for some magic to happen with the user input in the background.
我希望发生的事情是在后台用户输入时发生一些魔法。 In my own scenario, I would like to take a URL, then use bs4 to pick off what I need and display this on the web page.
在我自己的场景中,我想获取一个 URL,然后使用 bs4 来挑选我需要的内容并将其显示在网页上。
For simplicity, I'll ask for something for simple and I can then build on it from there: if I were to request the user to specify a number then press 'Submit', how could I multiply the number by 10?为简单起见,我会要求一些简单的东西,然后我可以从那里开始:如果我要求用户指定一个数字,然后按“提交”,我如何将数字乘以 10?
If my code for the form was index.html
:如果我的表单代码是
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>
I noticed that I can get the input to print to a paragraph by <p>form.request.GetNum</p>
.我注意到我可以通过
<p>form.request.GetNum</p>
将输入打印到一个段落。
Now for this question's example, the code for the backend functionality will be:现在对于这个问题的例子,后端功能的代码将是:
import math
GetNum = 10 #should be form.request.GetNum value
CalcResult = GetNum*1000
print CalcResult # or {{ CalcResult.data }} or something in index.html
My Controller ( app.py
) looks like:我的控制器(
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(..)
My worry is that the home
function will end up having a mass of code if I have to put the math function in there.我担心的是,如果我必须将数学函数放在那里,
home
函数最终会产生大量代码。 So tl;dr, how would I implement a back end function within my code?所以 tl;dr,我将如何在我的代码中实现后端功能? (or please provide me with material to read, thank you!)
(或者请给我提供阅读材料,谢谢!)
One other thing is I already have a form on my '/' page, will I have to rename the forms like form1 form2 because they will clash or will it be OK?另一件事是我的“/”页面上已经有一个表单,我是否必须重命名表单,例如 form1 form2 因为它们会发生冲突还是可以?
Elsewhere in your code base, either in the same file, or more likely a module or package you could define that complicated task.在你的代码库的其他地方,或者在同一个文件中,或者更有可能是一个模块或包,你可以定义那个复杂的任务。 Lets create a simple module
complicated.py
in the same directory as your other code, that then defines the complicated task:让我们在与其他代码相同的目录中创建一个简单的模块
complicated.py
,然后定义复杂的任务:
def do_really_complicated_thing(info):
# lots of complicated steps
return really_complicated_data
Then in our view code, we can just use that instead of having it embedded:然后在我们的视图代码中,我们可以使用它而不是嵌入它:
from complicated import do_really_complicated_thing
@app.route('/', methods=['GET', 'POST'])
@login_required
def home():
error = None
form = PostForm(request.form)
if form.validate_on_submit() :
info = form.info.data
complicated_task = do_really_complicated_thing(info)
return render_template('something', results=complicated_task)
So, in short-- the terms you're looking for is packages and modules, they help your code be neater and reusable.因此,简而言之——您正在寻找的术语是包和模块,它们帮助您的代码更整洁和可重用。
As for clashing forms— you can just target the form to post to a specific route which just handles that form, which is much cleaner then then having to validate/parse different forms in a single route.至于冲突表单——您可以将表单定位到只处理该表单的特定路由,这比在单个路由中验证/解析不同表单要干净得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.