【问题标题】:how a flask-python script calls another flask-python scriptflask-python 脚本如何调用另一个 flask-python 脚本
【发布时间】:2017-02-26 14:50:54
【问题描述】:

我是 flask-py2neo-pyhon-neo4j 的新手,所以我需要一些帮助 我有以下问题。我运行/执行的主要 .py 是 views.py 我有另一个 py 脚本,我在其中提交了一些 form_data.html> 更清楚 views.py --calls-->app.py--calls--> form_action.html & form_submit.html 我怎样才能实现这个序列? flask-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>

【问题讨论】:

    标签: python html web flask py2neo


    【解决方案1】:

    最简单的方法是在重构模板后简单地包含模板。 如果不重构它们,form_submit.html 中的内容将被添加,因此页面上会有两个&lt;html&gt;。 一个简单的重构可能是:

    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”。

    您可以在此处阅读有关烧瓶模板的信息:http://flask.pocoo.org/docs/0.12/patterns/templateinheritance/

    【讨论】:

    • 你好阿德里亚诺,我完全按照你说的做了,但是当我按下链接“你好”时,我收到错误“方法不允许 请求的 URL 不允许该方法。”
    • @app.route('/hello/', methods=['POST'])改成@app.route('/hello/', methods=['POST', 'GET'])
    【解决方案2】:

    不是,但我找到了! :) 非常感谢你,我很感激你提供了一些时间安德里亚诺 我不得不将变量从一个更改为另一个 我把它改成了 hello --> vital, form_submit-->vital.html 这是代码 视图.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>
    

    重要的.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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 2013-06-01
      • 2019-05-01
      • 1970-01-01
      • 2016-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多