2017-02-26 164 views
1

我是flask-py2neo-pyhon-neo4j中的新成員,因此我需要一些幫助 我遇到以下問題。我運行/執行的主要.py是views.py我有另一個py腳本,我已經提交了一些form_data.html>更清晰 views.py --calls - > app.py - calls-- > form_action.html & form_submit.html 我怎樣才能達到這個順序? 燒瓶-Python代碼:views.pyflask-python腳本如何調用另一個flask-python腳本

@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> 

回答

1

最簡單的方法僅僅是包括在重構它們之後,請使用模板。 如果您不重構它們,則會添加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」。

您可以在這裏燒瓶閱讀有關模板:http://flask.pocoo.org/docs/0.12/patterns/templateinheritance/

+0

你好阿德里亞諾,我做了完全一樣的你說的,但當我按下鏈接「你好」時,我得到的錯誤「方法不允許 該方法不允許請求的URL。 – Antwnina

+0

將'@ app.route('/ hello /',methods = ['POST'])'改爲'@ app.route('/ hello /',methods = ['POST','GET'])' –

0

沒有,但我找到了! :) 非常感謝你非常多,我很感激你提出了一些自己的時間。Andriano 我不得不改變一個變量到另一個 我已經改變它打招呼的一點insteed - >至關重要,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> 
相關問題