flask-python 脚本如何调用另一个 flask-python 脚本
how a flask-python script calls another flask-python script
我是 flask-py2neo-pyhon-neo4j 的新手,所以我需要一些帮助
我有以下问题。我的主要 .py runs/executes 是 views.py 我有另一个 py 脚本,我在其中提交了一些 form_data.html> 更清楚
views.py --calls-->app.py--calls--> form_action.html & form_submit.html
我怎样才能实现这个序列??
烧瓶-python 代码:views.py
@app.route('/vital', methods=['GET','POST'])
def vital():
# username = session.get('username')
name=request.form['pname']
sfx=request.form['psfx']
midname=request.form['pmidname']
bsurname=request.form['pbsurname']
# if not name:
# flash('You must give your post a name.')
# elif not sfx:
# flash('You must give your post a suffix.')
# elif not midname:
# flash('You must give your post a midname.')
# elif not bsurname:
# flash('You must give your post a bsurname.')
# else:
# User(session['username']).vital(name, sfx, midname, bsurname)
return render_template('form_action.html', username=username, sfx=sfx, midname=midname, bsurname=bsurname)
app.py
from flask import Flask, render_template, request, url_for
app = Flask(__name__)
@app.route('/')
def form():
return render_template('form_submit.html')
@app.route('/hello/', methods=['POST'])
def hello():
name=request.form['pname']
sfx=request.form['psfx']
midname=request.form['pmidname']
bsurname=request.form['pbsurname']
return render_template('form_action.html', name=name, sfx=sfx, midname=midname, bsurname=bsurname)
if __name__ == '__main__':
app.run()
send_data()
form_action.html
<html>
<head>
<title>Person</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div id="container">
<div class="title">
<h1>Person's Vital Information</h1>
</div>
<div id="content">
Tell us about <strong>{{name}}</strong> {{bsurname}}!
</div>
<div class="title">
<h1>Flask code</h1>
</div>
<code><pre>
@app.route('/hello/', methods=['POST'])
def hello():
name=request.form['pname']
sfx=request.form['psfx']
midname=request.form['pmidname']
bsurname=request.form['pbsurname']
return render_template('form_action.html', name=name, sfx=sfx, midname=midname, bsurname=bsurname)
</pre></code>
</div>
</div>
</body>
</html>
form_submit.html
<html>
<head>
<title>Person</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div id="container">
<div class="title">
<h1>Person's Vital Information</h1>
</div>
<div id="content">
<form method="post" action="{{ url_for('hello') }}">
<label for="pname">Name:</label>
<input type="text" name="pname" /><br />
<label for="psfx">Suffix: </label>
<input type="text" name="psfx" /><br />
<label for="pmidname">Middle Name: </label>
<input type="text" name="pmidname" /><br />
<label for="pbsurname">Birth Surname: </label>
<input type="text" name="pbsurname" /><br />
<input type="submit" />
</form>
</div>
<div class="title">
<h1>Flask code</h1>
</div>
<code><pre>
@app.route('/')
def form():
return render_template('form_submit.html')
</pre></code>
</div>
</div>
</body>
</html>
最简单的方法是在重构模板后简单地包含模板。
如果您不重构它们,将添加 form_submit.html 中的内容,因此页面上将有两个 <html>
。
一个简单的重构可以是:
base.html(新文件)
<html>
<head>
<title>{% block page_title %}{% endblock page_title %}</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
{% block page_content %}{% endblock page_content %}
</body>
</html>
form_submit.html(重构)
<form method="post" action="{{ url_for('hello') }}">
<label for="pname">Name:</label>
<input type="text" name="pname" /><br />
<label for="psfx">Suffix: </label>
<input type="text" name="psfx" /><br />
<label for="pmidname">Middle Name: </label>
<input type="text" name="pmidname" /><br />
<label for="pbsurname">Birth Surname: </label>
<input type="text" name="pbsurname" /><br />
<input type="submit" />
</form>
form_action.html(重构)
Tell us about <strong>{{name}}</strong> {{bsurname}}!
form_page.html(新文件)
{% extends "base.html" %}
{% block page_content %}
Content of form_action: <br/>
{% include "form_action.html" %}
<br/>
<br/>
Content of form_submit: <br/>
{% include "form_submit.html" %}
{% endblock page_content %}
另请注意,您现在应该在代码中呈现 "form_page.html"。
您可以在此处阅读有关 Flask 模板的信息:http://flask.pocoo.org/docs/0.12/patterns/templateinheritance/
不是,但我找到了! :)
非常感谢,我很感激你能抽出一些时间安德里亚诺
我不得不将变量从一个更改为另一个
我已经稍微改变了它而不是你好 --> 重要的,form_submit-->vital.html
这是代码
views.py
@app.route('/vital')
def vital():
return render_template('vital.html')
@app.route('/result',methods = ['POST', 'GET'])
def result():
name=request.form['pname']
sfx=request.form['psfx']
midname=request.form['pmidname']
bsurname=request.form['pbsurname']
return render_template('form_action.html',
name=name,
sfx=sfx,
midname=midname,
bsurname=bsurname
)
form_action.html
<!doctype html>
<html>
<body>
<!-- <blockquote> </blockquote> kanei kai tab k new line!! -->
<div id="content">{% block content %}
Tell us more about <strong>{{name}}</strong> {{bsurname}}!
<br /><br /><br /><i>You've entered: </i><br />
<i style="margin-left: 40px">{{midname}}</i><br />
<i style="margin-left: 40px">{{bsurname}}</i><br /><br /><br /><br /><br /><br /><br />
{% endblock %}</div>
</div>
</body>
</html>
vital.html
<html>
<body>
<div id="container">
<div class="title">
<h1>Person's Vital Information</h1>
</div>
<div id="content">
<form action = "http://localhost:5000/result" method = "POST">
<label for="pname">Name:</label>
<input type="text" name="pname" /><br />
<label for="psfx">Suffix: </label>
<input type="text" name="psfx" /><br />
<label for="pmidname">Middle Name: </label>
<input type="text" name="pmidname" /><br />
<label for="pbsurname">Birth Surname: </label>
<input type="text" name="pbsurname" /><br />
<input type="submit" />
</form>
</div>
<div class="title">
</div>
</div>
</body>
</html>
我是 flask-py2neo-pyhon-neo4j 的新手,所以我需要一些帮助 我有以下问题。我的主要 .py runs/executes 是 views.py 我有另一个 py 脚本,我在其中提交了一些 form_data.html> 更清楚 views.py --calls-->app.py--calls--> form_action.html & form_submit.html 我怎样才能实现这个序列?? 烧瓶-python 代码:views.py
@app.route('/vital', methods=['GET','POST'])
def vital():
# username = session.get('username')
name=request.form['pname']
sfx=request.form['psfx']
midname=request.form['pmidname']
bsurname=request.form['pbsurname']
# if not name:
# flash('You must give your post a name.')
# elif not sfx:
# flash('You must give your post a suffix.')
# elif not midname:
# flash('You must give your post a midname.')
# elif not bsurname:
# flash('You must give your post a bsurname.')
# else:
# User(session['username']).vital(name, sfx, midname, bsurname)
return render_template('form_action.html', username=username, sfx=sfx, midname=midname, bsurname=bsurname)
app.py
from flask import Flask, render_template, request, url_for
app = Flask(__name__)
@app.route('/')
def form():
return render_template('form_submit.html')
@app.route('/hello/', methods=['POST'])
def hello():
name=request.form['pname']
sfx=request.form['psfx']
midname=request.form['pmidname']
bsurname=request.form['pbsurname']
return render_template('form_action.html', name=name, sfx=sfx, midname=midname, bsurname=bsurname)
if __name__ == '__main__':
app.run()
send_data()
form_action.html
<html>
<head>
<title>Person</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div id="container">
<div class="title">
<h1>Person's Vital Information</h1>
</div>
<div id="content">
Tell us about <strong>{{name}}</strong> {{bsurname}}!
</div>
<div class="title">
<h1>Flask code</h1>
</div>
<code><pre>
@app.route('/hello/', methods=['POST'])
def hello():
name=request.form['pname']
sfx=request.form['psfx']
midname=request.form['pmidname']
bsurname=request.form['pbsurname']
return render_template('form_action.html', name=name, sfx=sfx, midname=midname, bsurname=bsurname)
</pre></code>
</div>
</div>
</body>
</html>
form_submit.html
<html>
<head>
<title>Person</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div id="container">
<div class="title">
<h1>Person's Vital Information</h1>
</div>
<div id="content">
<form method="post" action="{{ url_for('hello') }}">
<label for="pname">Name:</label>
<input type="text" name="pname" /><br />
<label for="psfx">Suffix: </label>
<input type="text" name="psfx" /><br />
<label for="pmidname">Middle Name: </label>
<input type="text" name="pmidname" /><br />
<label for="pbsurname">Birth Surname: </label>
<input type="text" name="pbsurname" /><br />
<input type="submit" />
</form>
</div>
<div class="title">
<h1>Flask code</h1>
</div>
<code><pre>
@app.route('/')
def form():
return render_template('form_submit.html')
</pre></code>
</div>
</div>
</body>
</html>
最简单的方法是在重构模板后简单地包含模板。
如果您不重构它们,将添加 form_submit.html 中的内容,因此页面上将有两个 <html>
。
一个简单的重构可以是:
base.html(新文件)
<html>
<head>
<title>{% block page_title %}{% endblock page_title %}</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
{% block page_content %}{% endblock page_content %}
</body>
</html>
form_submit.html(重构)
<form method="post" action="{{ url_for('hello') }}">
<label for="pname">Name:</label>
<input type="text" name="pname" /><br />
<label for="psfx">Suffix: </label>
<input type="text" name="psfx" /><br />
<label for="pmidname">Middle Name: </label>
<input type="text" name="pmidname" /><br />
<label for="pbsurname">Birth Surname: </label>
<input type="text" name="pbsurname" /><br />
<input type="submit" />
</form>
form_action.html(重构)
Tell us about <strong>{{name}}</strong> {{bsurname}}!
form_page.html(新文件)
{% extends "base.html" %}
{% block page_content %}
Content of form_action: <br/>
{% include "form_action.html" %}
<br/>
<br/>
Content of form_submit: <br/>
{% include "form_submit.html" %}
{% endblock page_content %}
另请注意,您现在应该在代码中呈现 "form_page.html"。
您可以在此处阅读有关 Flask 模板的信息:http://flask.pocoo.org/docs/0.12/patterns/templateinheritance/
不是,但我找到了! :) 非常感谢,我很感激你能抽出一些时间安德里亚诺 我不得不将变量从一个更改为另一个 我已经稍微改变了它而不是你好 --> 重要的,form_submit-->vital.html 这是代码 views.py
@app.route('/vital')
def vital():
return render_template('vital.html')
@app.route('/result',methods = ['POST', 'GET'])
def result():
name=request.form['pname']
sfx=request.form['psfx']
midname=request.form['pmidname']
bsurname=request.form['pbsurname']
return render_template('form_action.html',
name=name,
sfx=sfx,
midname=midname,
bsurname=bsurname
)
form_action.html
<!doctype html>
<html>
<body>
<!-- <blockquote> </blockquote> kanei kai tab k new line!! -->
<div id="content">{% block content %}
Tell us more about <strong>{{name}}</strong> {{bsurname}}!
<br /><br /><br /><i>You've entered: </i><br />
<i style="margin-left: 40px">{{midname}}</i><br />
<i style="margin-left: 40px">{{bsurname}}</i><br /><br /><br /><br /><br /><br /><br />
{% endblock %}</div>
</div>
</body>
</html>
vital.html
<html>
<body>
<div id="container">
<div class="title">
<h1>Person's Vital Information</h1>
</div>
<div id="content">
<form action = "http://localhost:5000/result" method = "POST">
<label for="pname">Name:</label>
<input type="text" name="pname" /><br />
<label for="psfx">Suffix: </label>
<input type="text" name="psfx" /><br />
<label for="pmidname">Middle Name: </label>
<input type="text" name="pmidname" /><br />
<label for="pbsurname">Birth Surname: </label>
<input type="text" name="pbsurname" /><br />
<input type="submit" />
</form>
</div>
<div class="title">
</div>
</div>
</body>
</html>