Difference between run the Flask and Django application

Question:

I don’t understand why if I want to run Flask application I need

(venv) $ export FLASK_APP=microblog.py
(venv) $ flask run

But if I want to run Django application I only

(venv) $ python manage.py runserver

without export DJANGO_APP=microblog.py
Why? Why I need the export app in the first case, but in the second case I don’t need?

Asked By: Serhii

||

Answers:

Firstly, Django and Flask are different frameworks. There’s no reason why the commands to start them should be the same.

You need to export FLASK_APP to tell flask which app to run.

Doing export FLASK_APP=microblog.py sets an environment variable FLASK_APP. The flask application can then read this variable from the environment and use it to run the application.

In Python you can access environment variables from os.environ, or use the os.getenv method:

import os
flask_app = os.getenv('FLASK_APP')

If you use the django-admin command, you need to export DJANGO_SETTINGS_MODULE in a similar way:

$ export DJANGO_SETTINGS_MODULE=yourproject.settings
$ django-admin runserver

However with Django, you usually use runserver with manage.py instead of django-admin. The manage.py is specific to your project and sets the DJANGO_SETTINGS_MODULE environment variable if it hasn’t been set already:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourproject.settings")

Therefore you don’t need to export DJANGO_SETTINGS_MODULE when using manage.py.

Answered By: Alasdair

Problem: When you write the command flask run in the console, how does flask know which file to run?

Solution: Thats why we use export FLASK_APP=microblog.py

It is setting the FLASK_APP (an internal flask variable) value to microblog.py

It tells flask to use microblog.py as start-up file for the application when you run flask via flask run command.

If you decide not to do so, then when you run flask run, then there is no way for flask to know which file to run. Now you could run the application using python filename.py instead of flask run

So python microblog.py in your case.

Answered By: variable

From a source code prospective, seems that the FLASK_APP variabile is used for understand which flask app run in a ‘multi-flask-app’ environment.

Is necessary only if the app_name from the source is not present.

The FLASK_APP env variable is used only into the find_best_app method of the cli.py file of the flask framework.

Answered By: alessiosavi

Simply in your project, you don’t need something like export FLASK_APP=microblog.py unless you want environment variables

from flask import jsonify,Flask

app = Flask(__name__)

@app.route("/<Your Route>/<string:<Your Param>>")
def main(<Your Param>):

    //DO LOGIC HERE 
    data =[{'TestData1' : "" ,<YOUR OUTPUT>}]

    return jsonify(data), 200

app.run(debug=False,host="0.0.0.0",port=<PORT YOU WANT TO HOST>)  
Answered By: Ashane.E
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.