1

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>
Antwnina
  • 117
  • 12

2 Answers2

1

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/

Adriano Martins
  • 1,788
  • 1
  • 19
  • 23
  • Hello Adriano, I did exactly as you said but when I press to link "Hello" I get the error "Method Not Allowed The method is not allowed for the requested URL." – Antwnina Feb 28 '17 at 10:14
  • change the `@app.route('/hello/', methods=['POST'])` to `@app.route('/hello/', methods=['POST', 'GET'])` – Adriano Martins Feb 28 '17 at 13:30
0

neither that, but i found it! :) THANK YOU VERY VERY MUCH AND I APPRECIATE IT YOU OFFERED SOME OF YOUR TIME Andriano i had to change the variables from one to another I've change it a bit insteed of hello --> vital, form_submit-->vital.html here's the code 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>
Antwnina
  • 117
  • 12