0

I'm able to get data from my form and store it in my postgres database but I want to post an error for the user if the submitted form is incomplete.

I'm sure this is very trivial but for some reason I get an unhelpful Bad Request 400 error (with no traceback) if I submit an incomplete form. I am having a tough time figuring out what is wrong with my code. Below is my flask view, my template, html.

I'd really appreciate any help - thank you.

[Re Duplication Note - I did see the other question when I posted this, but unfortunately I don't understand how they are the same as that user had two submit buttons whereas I submit only once. That response addresses how to get both buttons to work. If I am missing something, please let me know. Thanks.]

@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>
Warren Dew
  • 8,790
  • 3
  • 30
  • 44
Radha Kapoor
  • 271
  • 2
  • 6
  • 14
  • You shall see some POST related request in your access log, possibly visible on your console, where you have started Flask. What url it is exactly reporting? It is likely, you are posting to different url, then you think. – Jan Vlcinsky Jun 09 '14 at 00:09
  • When it gets my form the url is: "GET /logevent HTTP/1.1" 200 When I submit the form fully completed it is "POST /logevent HTTP/1.1" 200 - and when it is incomplete it is: "POST /logevent HTTP/1.1" 400 - – Radha Kapoor Jun 09 '14 at 14:05

0 Answers0