0

I have a page index.html that has to send two values to main.html. From index.html, I send an ajax request to /api/session_load with a json object data. I call the redirect function to main

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/main')    
def main(names):

    print('Hello-main')
    print('main', names, type(names))

    return render_template('main.html', nomens = names)

@app.route('/api/session_load', methods=['GET', 'POST'])
def session_load():
    if request.method == "POST":
        data = json.loads(request.get_data())
    global session_name
    global file_name
    session_name = data['session_name']
    file_name = data['file_name']

    print('sess', data, type(data))

    return redirect(url_for('main', names = data))

This is the output

* Restarting with stat
 * Debugger is active!
 * Debugger PIN: 147-211-412
sess {'file_name': 'sample.txt', 'session_name': 'asfds'} <class 'dict'>
127.0.0.1 - - [09/May/2019 00:04:32] "POST /api/session_load HTTP/1.1" 302 -
127.0.0.1 - - [09/May/2019 00:04:32] "GET /main?names=%7B%27file_name%27%3A+%27sample.txt%27%2C+%27session_name%27%3A+%27asfds%27%7D HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Users/300041738/.arun-venv3/lib/python3.7/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/300041738/.arun-venv3/lib/python3.7/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/300041738/.arun-venv3/lib/python3.7/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/300041738/.arun-venv3/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Users/300041738/.arun-venv3/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/300041738/.arun-venv3/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/300041738/.arun-venv3/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/300041738/.arun-venv3/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Users/300041738/.arun-venv3/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/300041738/.arun-venv3/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
TypeError: main() missing 1 required positional argument: 'names'

main.html file has proper templating with {{ nomens }}. Both statements of main() don't print.

canonball
  • 515
  • 7
  • 22

1 Answers1

0

@app.route('/main') should be @app.route('/main/<string:names>'). You need to tell flask the url is expecting something else after your route name.

Frederik Bode
  • 2,632
  • 1
  • 10
  • 17
  • 2
    actually, I wish to use the url_for function and be able to pass around dict objects so as to render templates with a lot of variable info, how do i achieve that – canonball May 08 '19 at 19:00
  • You can pass around the json payload of the original request by calling `redirect(url_for('main'), code=307)`, see [here] (https://stackoverflow.com/questions/15473626/make-a-post-request-while-redirecting-in-flask). You would need to re-parse the data though, and this doesn't allow to change the data though. Is that a problem? – Frederik Bode May 08 '19 at 19:21