Django vs Flask

Django & Flask, which one is better?

The ANSWER is : IT DEPENDS.

'Hello world'

Hello world in Flask:
from flask import Flask

app = Flask(__name__)


@app.route('/aa')
def hello_world():
    return 'Hello World!'


if __name__ == '__main__':
    app.run()

Hello world in Django:
image.png
Django vs Flask_第1张图片
image.png

Comparsion

Django is more intimidating to beginners than Flask

Django has a steeper learning curve: settings, regular expressions, etc

Flask allows single-file projects

Data models

Data models in Django
Django vs Flask_第2张图片
image.png
Django vs Flask_第3张图片
image.png
Data models in Flask

Flask doesn't have data models!

Data Modeling

image.png
Flask is Extensible

Flask is intentionally minimalist.

Flask includes templating, URL routing, error handling, and a debugger. That's all.

All other functionality is delegated to extensions, Pick and choose the functionality that you want!

Comparsion

Django's data models are easier to get started: they are built-in to the framework.

Django assumes that you will use a relational database. If you don't, it will fight you.

Flask allows more flexibility to choose your data model. More choices mean more potential to screw something up. :(

Apps

Apps in Django:

settings.INSTALLED_APPS

Django Packages

Many packages available; hard to know which are good to use;

Hard to organize an existing project into multiple apps

Blueprints in Flask

Not quite the same as an app: blueprints are instructions for how to extend an existing app

Can be applied multiple times to the same app in different ways

Optional, but recommended for larger Flask projects

Familiar syntax, easy to get started

Comparsion

Django apps are more comprehensive, more numerous - but also more complex

Flsk blueprints are simpler, easier to integrate into a project

APIs

APIs

APIs in Django

Django REST Framework. Just use it

Authentication policies, serializers, extensive documentation, testing tools... it's all included

Multi-layered abstractions

APIs in Flask

Multiple extensions working together

Serialization: " Marshmallow " module

Marshmallow ecosystem includes integrations with Flask, SQLALchemy, MongoEngine, etc


Django vs Flask_第4张图片
image.png

Comparison

Django REST Framework is amazing, but is subject to the same restrictions as Django itself(relational database, etc)

Flask has all the same functionality with much more flexibility, but you have to put it together yourself.

Maybe someday there will be an extension bundle for Flask that is similar to DRF: not yet

Advice

Choose Django when...

You're happy with all the choices Django makes for you:
Django ORM, Django templates, etc

You're not doing anything unusual

You don't care to learn the details of how things work, you just want something that works

Choose Flask when...

You disagree with one of Django's choices, and want to do things differently

You have unusual requirements that require custom components

You want to understand how plumbing of your applications fits together

你可能感兴趣的:(Django vs Flask)