Flask Bad Request 400错误

时间:2014-06-08 22:51:21

标签: python postgresql flask

我可以从我的表单中获取数据并将其存储在我的postgres数据库中,但如果提交的表单不完整,我想为用户发布错误。

我确定这非常简单,但出于某种原因,如果我提交的表格不完整,我会收到一个无用的Bad Request 400错误(没有追溯)。我很难搞清楚我的代码有什么问题。下面是我的烧瓶视图,我的模板,html。

我真的很感激任何帮助 - 谢谢。

[重复复制说明 - 当我发布这个时我确实看到了另一个问题,但不幸的是我不明白他们是如何与用户有两个提交按钮相同,而我只提交一次。该响应解决了如何使两个按钮工作。如果我遗失了什么,请告诉我。感谢。]

@app.route('/logevent', methods=['POST'])
def post_logevent():
    error = None    
    A = (request.form['A'])
    B = (request.form['B'])
    C = (request.form['C'])
    D = (request.form['D'])
    E = (request.form['E'])      
    if A is not None and B is not None and C is not None and D is not None and E is not None:
        event = Event(A=A, B=B, C=C, D=D, E=E)        
        g.user.events.append(event)        
        db.session.commit()
        return render_template("submit.html", A=A, B=B, C=C, D=D, E=E, error=error)
    else:
        return render_template("submit.html", A="", B="", C="", D="", 
        E="", error="Error! Your form isn't complete!") 

submit.html

{% block content %}

{% if error %} 
{{ error }}
{% else %}

You just logged an event at {{ A }} with {{ B }} and you felt {{ C }} before the event, {{ D }} during the event and {{ E }} afterwards. 


{% endif %}

{% endblock %}

logevent.html

<form action="{{ url_for('post_logevent') }}" method='post'>      
  <h3>2. A</h3>
  <select name='A'>
      <option value='1'>1</option>
      <option value='2'>2</option>
      <option value='3'>3</option>        
  </select>
  <br>

  <h3>3. B</h3>
  <select name='B'>
      <option value='Inside'>Inside</option>
      <option value='Outside'>Outside</option>
      <option value='Poolside'>Poolside</option>
  </select>
  <br>
  <h3>4. C</h3>

  <input type='radio' name='C' value='1'>1 
  <input type='radio' name='C' value='2'>2
  <input type='radio' name='C' value='3'>3  


  <h3>5. D</h3>

  <input type='radio' name='D' value='1'>1
  <input type='radio' name='D' value='2'>2
  <input type='radio' name='D' value='3'>3    


  <h3>6. E</h3> 

  <input type='radio' name='E' value='1'>1 
  <input type='radio' name='E' value='2'>2
  <input type='radio' name='E' value='3'>3    

  <br>
  <input type='submit' value='submit'>
 </form>

0 个答案:

没有答案
相关问题