2

Currently I'm trying to link the submit button to the 'page1' page however im getting a bad request error, originally I thought the issue was to do with the POST part of the method but wasn't clear oin how i can fix this issue, To sum this up in a more precise way, I;m trying to get to PAGE1 from PAGE using the submit button below but im getting

"Bad Request The browser (or proxy) sent a request that this server could not understand."

View.py

@expose('/page', methods=['GET', 'POST'])
@appbuilder.app.route('/page', methods=['GET', 'POST'])
def page(self):

    if request.method == 'POST':
        bucket = request.form['bucket']
        session['bucket'] = bucket
        return redirect(url_for('view.files'))
    else:
        return render_template(
            'x.html',
            base_template=appbuilder.base_template,
            appbuilder=appbuilder
        )

@expose('/page1', methods=['GET', 'POST'])
@appbuilder.app.route('/page1', methods=['GET', 'POST'])
def page1(self):

    if request.method == 'POST':
        bucket = request.form['bucket']
        session['bucket'] = bucket
        return redirect(url_for('view.files'))
    else:
        buckets = get_buckets_list()
        return render_template(
            'x1.html',
            base_template=appbuilder.base_template,
            appbuilder=appbuilder,
            buckets=buckets
        )

HTML page

            <td>
                <form class="select-bucket" action="{{ url_for('view.page1')}}" method="POST">
                    <button type="submit" name="submit" value="submit">Submit</button>
                </form>
            </td>

1 Answers1

0

In def page(self), you have:

    return redirect(url_for('view.files'))

It should be

    return redirect(url_for('page1'))

so it points to your page1 function.

Solution from: https://teamtreehouse.com/community/flask-redirect-vs-redirecturlfor

nio
  • 5,141
  • 2
  • 24
  • 35
  • it doesnt change the error, this is what its saying in the command prompt 2018-08-20 12:04:14,695:INFO:werkzeug:127.0.0.1 - - [20/Aug/2018 12:04:14] "POST /home/page1 HTTP/1.1" 400 - @nio –  Aug 20 '18 at 11:03
  • check for any code throwing exceptions in `def page` and `def page1` (https://stackoverflow.com/questions/19578613/posting-data-on-flask-via-form-is-giving-400-bad-request) – nio Aug 20 '18 at 11:31