2017-10-16 107 views
-2

嗨,我是python瓶的新手,我有什麼似乎是一個400錯誤的請求錯誤的簡單問題,這應該是由於在嘗試將它們傳遞給函數時錯誤地命名了變量中的變量在蟒蛇燒瓶。我已經做了一些研究,但我仍然無法弄清楚這個代碼出錯的地方,任何幫助都會得到真正的讚賞。 下面是HTML表單400錯誤的請求python瓶

<html> 
<body> 

    <h1>Add a Munroe to your list</h1> 
    <form action = "{{ url_for('addmunro') }}" method="POST" 
     enctype = "multipart/form-data"> 

     Name<br> 
     <input type="text" name="mnName"/><br> 
     Description<br> 
     <input type="text" name="mnDesc"/><br> 
     Region<br> 
     <input type="text" name="mnRegion"/><br> 
     Height<br> 
     <input type="text" name="mnHeight"/><br> 
     Walk date<br> 
     <input type="text" name="mnDate"/><br> 
     Walk image<br> 
     <input type="text" name="mnImage"/><br> 

     <br> 
     <br> 
     <input type="submit" name="add-munro.html" value = "ADD MUNRO"/> 

    </form> 
</body> 
</html> 

這裏的代碼是Python的燒瓶應用

 from flask import Flask, render_template, url_for, redirect, json, request 
app = Flask(__name__) 

@app.route('/add-munro.html', methods=['GET']) 
def addmunro(): 


    #Create an empty list 
    mnList={} 

    #Create a munro dictionary 
    munro = {'name':request.form['mnName'], 
      'desc':request.form['mnDesc'], 
      'region':request.form['mnRegion'], 
      'height':request.form['mnHeight'], 
      'date':request.form['mnDate'], 
      'image':request.form['mnImage']} 

    #the munro dictionary is added to mnList 
    #mnList.append(munro) 

    return render_template('add-munro.html') 


if __name__ == "__main__": 
    app.run(host = '0.0.0.0', debug = True) 

回答

0

有以下幾種錯誤:

  1. 您正在POST請求,但只有處理GET請求不需要路由
  2. 蒙羅對象未傳遞給模板
  • 的.html

    我已更新這些,現在它很好去:

    application.py

    from flask import Flask, render_template, request, url_for 
    
    app = Flask(__name__)  
    
    @app.route('/add-munro', methods=['GET','POST']) 
    def addmunro(): 
        if request.method == "POST": 
         #Create an empty list 
         mnList={} 
         #Create a munro dictionary 
         munro = {'name':request.form['mnName'], 
           'desc':request.form['mnDesc'], 
           'region':request.form['mnRegion'], 
           'height':request.form['mnHeight'], 
           'date':request.form['mnDate'], 
           'image':request.form['mnImage']} 
         return render_template('add-munro.html', munro=munro) 
        else: 
         return render_template('add-munro.html') 
    
    if __name__ == '__main__': 
        app.run(debug=True) 
    

    add-munro.html

    <html> 
    <body> 
        {% if munro is defined -%} 
        <h3> 
         Name: {{ munro.name }} 
        </h3> 
        <h3> 
         Description: {{ munro.desc }} 
        </h3> 
        <h3> 
         Region: {{ munro.region }} 
        </h3> 
        <h3> 
         Height: {{ munro.height }} 
        </h3> 
        {%- endif %} 
        <h1>Add a Munroe to your list</h1> 
        <form action = "{{ url_for('addmunro') }}" method="POST" 
        enctype = "multipart/form-data"> 
    
        Name<br> 
        <input type="text" name="mnName"/><br> 
        Description<br> 
        <input type="text" name="mnDesc"/><br> 
        Region<br> 
        <input type="text" name="mnRegion"/><br> 
        Height<br> 
        <input type="text" name="mnHeight"/><br> 
        Walk date<br> 
        <input type="text" name="mnDate"/><br> 
        Walk image<br> 
        <input type="text" name="mnImage"/><br> 
    
        <br> 
        <br> 
        <input type="submit" value = "ADD MUNRO"/> 
    </form> 
    </body> 
    </html> 
    

    輸出:

    enter image description here

  • +0

    感謝您的解釋,我也需要上傳圖片作爲字典的一部分,我知道我必須改變在html中的文件類型,但我如何保存在燒瓶應用程序的字典中? – costep

    +0

    您無法將文件保存在字典中。但是,您可以將文件保存在一個目錄中,然後將該路徑保存在字典中。使用'image = request.files [「mnImage」]'獲取控制器中的文件,然後使用'image.save()'將其保存在目錄中。最後,您可以返回字典中圖像的路徑。 – arsho

    1

    你快把你的形式POST請求的代碼,但只允許在GET方法您的應用路線。將@app.route('/add-munro.html', methods=['GET'])更改爲@app.route('/add-munro.html', methods=['POST'])