Questions tagged [flask]

Flask is a lightweight framework for developing web applications using Python.

Flask is a web application framework for . It has exceptional documentation, a large number of extensions and a friendly community on Discord. It is open source and BSD-3-Clause licensed.

Resources

Related Tags

54068 questions
316
votes
4 answers

How to get http headers in flask?

Using Flask, how can I read HTTP headers? I want to check the authorization header which is sent by the client.
emil
  • 6,074
  • 4
  • 30
  • 38
266
votes
7 answers

Passing HTML to template using Flask/Jinja2

I'm building an admin for Flask and SQLAlchemy, and I want to pass the HTML for the different inputs to my view using render_template. The templating framework seems to escape the HTML automatically, so all <"'> characters are converted to HTML…
sharvey
  • 7,635
  • 7
  • 48
  • 66
262
votes
6 answers

Create dynamic URLs in Flask with url_for()

Half of my Flask routes requires a variable say, //add or //remove. How do I create links to those locations? url_for() takes one argument for the function to route to but I can't add arguments?
Gio Borje
  • 20,314
  • 7
  • 36
  • 50
252
votes
3 answers

Can I serve multiple clients using just Flask app.run() as standalone?

I know I can link Flask with Apache or other web servers. But, I was thinking of running Flask as a standalone server serving multiple clients simultaneously. Is this possible? Do I have to handle spawning multiple threads and managing them?
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
252
votes
6 answers

How to divide flask app into multiple py files?

My flask application currently consists of a single test.py file with multiple routes and the main() route defined. Is there some way I could create a test2.py file that contains routes that were not handled in test.py? @app.route('/somepath') def…
Rolando
  • 58,640
  • 98
  • 266
  • 407
236
votes
8 answers

Python Flask, how to set content type

I am using Flask and I return an XML file from a get request. How do I set the content type to xml ? e.g. @app.route('/ajax_ddl') def ajax_ddl(): xml = 'foo' header("Content-type: text/xml") return xml
Tampa
  • 75,446
  • 119
  • 278
  • 425
229
votes
5 answers

How do I get the different parts of a Flask request's url?

I want to detect if the request came from the localhost:5000 or foo.herokuapp.com host and what path was requested. How do I get this information about a Flask request?
Dogukan Tufekci
  • 2,978
  • 3
  • 17
  • 21
227
votes
6 answers

Typical AngularJS workflow and project structure (with Python Flask)

I am pretty new to this whole MV* client-side framework frenzy. It doesn't have to be AngularJS, but I picked it because it feels more natural to me than either Knockout, Ember or Backbone. Anyway what is the workflow like? Do people start with…
Sahat Yalkabov
  • 32,654
  • 43
  • 110
  • 175
224
votes
5 answers

What are Flask Blueprints, exactly?

I have read the official Flask documentation on Blueprints and even one or two blog posts on using them. I've even used them in my web app, but I don't completely understand what they are or how they fit into my app as a whole. How is it similar to…
JoshieSimmons
  • 2,721
  • 2
  • 14
  • 23
224
votes
18 answers

Flask raises TemplateNotFound error even though template file exists

I am trying to render the file home.html. The file exists in my project, but I keep getting jinja2.exceptions.TemplateNotFound: home.html when I try to render it. Why can't Flask find my template? from flask import Flask, render_template app =…
Srdan Ristic
  • 3,203
  • 5
  • 18
  • 23
223
votes
4 answers

How to delete a record by id in Flask-SQLAlchemy

I have users table in my MySql database. This table has id, name and age fields. How can I delete some record by id? Now I use the following code: user = User.query.get(id) db.session.delete(user) db.session.commit() But I don't want to make any…
Sergey
  • 5,396
  • 3
  • 26
  • 38
218
votes
2 answers

When should Flask.g be used?

I saw that g will move from the request context to the app context in Flask 0.10, which made me confused about the intended use of g. My understanding (for Flask 0.9) is that: g lives in the request context, i.e., created afresh when the requests…
Yaniv Aknin
  • 4,103
  • 3
  • 23
  • 29
215
votes
13 answers

How to debug a Flask app

How are you meant to debug errors in Flask? Print to the console? Flash messages to the page? Or is there a more powerful option available to figure out what's happening when something goes wrong?
Kimmy
  • 3,787
  • 5
  • 22
  • 20
214
votes
12 answers

How to enable CORS in flask

I am trying to make a cross origin request using jquery but it keeps being reject with the message XMLHttpRequest cannot load http://... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ... is therefore not…
Lopes
  • 2,257
  • 2
  • 13
  • 7
212
votes
12 answers

Return HTTP status code 201 in flask

We're using Flask for one of our API's and I was just wondering if anyone knew how to return a HTTP response 201? For errors such as 404 we can call: from flask import abort abort(404) But for 201 I get LookupError: no exception for 201 Do I need…
ingh.am
  • 25,981
  • 43
  • 130
  • 177