python - How to detect bugs on POST request(flask)? -


i'm new flask. trying apply post/redirect/get pattern program. here did.

in index.html

{% block page_content %} <div class="container">     <div class="page-header">         <h1>hello, {% if user %} {{ user }} {% else %} john doe {% endif %}: {% if age %} {{ age }} {% else %} ?? {% endif %}</h1>     </div> </div>     {% if form %}     {{wtf.quick_form(form)}}     {% endif %} {% endblock %} 

in views.py

class nameform(form):     age = decimalfield('what\'s age?', validators=[required()])     submit = submitfield('submit')  '''''' @app.route('/user/<user>', methods=['get', 'post']) def react(user):     session['user'] = user     form = nameform()     if form.validate_on_submit():         old_age = session.get('age')         if old_age != none , old_age != form.age.data:             flash('age changed')             session['age'] = form.age.data         return redirect(url_for('react', user = user))     return render_template('index.html', user = user, age = session.get('age'), form = form, current_time = datetime.utcnow()) 

the request processed when open xxxx:5000/user/abc. however, post request fails. 404 error. think url_for function may give wrong value redirect. how can check value returned url_for?

i got 405 error when tried use database. time have no clue.

@app.route('/search', methods=['get', 'post']) def search():     form = searchform() # stringfield 'name' , submitfield     if form.validate_on_submit():         person = person.query.filter_by(name = form.name.data) # person table has 2 attributes 'name' , 'age'         if person none:             flash('name not found in database')         else:             session['age'] = person.age             return redirect(url_for('search'))     return render_template('search.html', form = form, age = session.get('age'), current_time = datetime.utcnow()) 

is there convenient way debug when post request fails?

the problem isn't url_for(), it's way you're using wtf.quick_form(). take @ form generated code:

<form action="." method="post" class="form" role="form"> 

the action="." line tells browser take information given , post url .. period (.) means "current directory." what's happening you're clicking submit, , browser posts localhost:5000/users/. flask sees request /users/ , cannot serve it, because /users/ isn't valid url. that's why you're getting 404.

fortunately, can fixed. in index.html, try calling quick_form() , passing in action:

{{wtf.quick_form(form, action=url_for('react', user=user))}} 

now, form rendered this:

<form action="/user/abc" method="post" class="form" role="form"> 

and browser knows post form /user/abc, valid url, flask handle it.

you didn't post code search.html, try applying same logic above template well; that'll fix issue!


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -