Skip to content

Tag: flask

redirect while passing arguments

In flask, I can do this: And if foo.html contains {{ messages[‘main’] }}, the page will show hello. But what if there’s a route that leads to foo: In this case, the only way to get to foo.html, if I want that logic to happen anyway, is through a redirect: So, how can I get that messages vari…

TypeError: ObjectId(”) is not JSON serializable

My response back from MongoDB after querying an aggregated function on document using Python, It returns valid response and i can print it but can not return it. Error: Print: But When i try to return: It is RESTfull call: db is well connected and collection is there too and I got back valid expected result b…

Flask 302 error code when using flask.redirect

I have this code https://github.com/italomaia/flask-empty/blob/master/src/0.8/main.py and I wrote at the end of the file: If I run this code, I get 302 server error (ERR_TOO_MANY_REDIRECTS), but if I change this line return redirect(url_for(‘login’)) by return ‘Hello!’ it works without…

how can i safely write to a file when using Flask?

I need to write a method on a Flask server which will write part of the request to a log file. However, if I understand correctly, Flask is multi-threaded, and there’s a good chance that writing to a file is not safe. Admittedly, I am more or less new to Python and multi-threaded programming in general,…

Get list of all routes defined in the Flask app

I have a complex Flask-based web app. There are lots of separate files with view functions. Their URLs are defined with the @app.route(‘/…’) decorator. Is there a way to get a list of all the routes that have been declared throughout my app? Perhaps there is some method I can call on the app…

Return JSON response from Flask view

I have a function that analyzes a CSV file with Pandas and produces a dict with summary information. I want to return the results as a response from a Flask view. How do I return a JSON response? Answer A view can directly return a Python dict or list and Flask will call jsonify automatically. For older Flask…

Sort dict in jinja2 loop

I’m still learning jinja2 and flask and I’m having a difficulty using dictsort in jinja2. So I’m passing this dict into a jinja2 template: What I want is create a table that is sorted by the value of the key ‘totalpts’. I tried all sort of things and it just doesn’t take to…

How to implement server push in Flask framework?

I am trying to build a small site with the server push functionality on Flask micro-web framework, but I did not know if there is a framework to work with directly. I used Juggernaut, but it seems to be not working with redis-py in current version, and Juggernaut has been deprecated recently. Does anyone has …

Pass variables to Flask’s render_template

I want to pass multiple variables from my Flask view to my Jinja template. Right now, I can only pass one. How do I pass multiple variable when rendering a template? Answer The render_template function takes any number of keyword arguments. Query for each of the things you need in the template, then pass the …