Search code examples
pythonhtmlwebflaskpy2neo

how a flask-python script calls another flask-python script


I'm new in flask-py2neo-pyhon-neo4j so I need some help I have the following issue. My main .py that runs/executes is views.py I have another py script where I have the submission of some form_data.html>to be more clear views.py --calls-->app.py--calls--> form_action.html & form_submit.html how can i achieve this sequence?? the flask-python code: 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>

Solution

  • The easiest approach is simply include the templates, after refactoring them. If you don't refactor them, the content from the form_submit.html will be added, therefore there will be two <html> on the page. A simple refactor could be:

    base.html (New File)

    <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 (Refactored)

    <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 (Refactored)

    Tell us  about <strong>{{name}}</strong> {{bsurname}}!
    

    form_page.html (New file)

    {% 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 %}
    

    Also note that you should render now the "form_page.html" on your code.

    You can read about templates on flask here: http://flask.pocoo.org/docs/0.12/patterns/templateinheritance/