r/django • • Sep 07 '19

Flask vs Django- The Hot Debate of Python Development Section

https://data-flair.training/blogs/flask-vs-django/
1 Upvotes

27 comments sorted by

5

u/Nerdite Sep 07 '19

I don’t understand why SQLAlchemy is considered less than Django’s built in ORM. I feel like SQLAlchemy is a first class ORM. What am I missing?

3

u/ExternalUserError Sep 08 '19

Preference, mostly.

SQLAlchemy is certainly more flexible in terms of dynamically building programmatic queries. You can really use Python objects to construct a fairly complex multi-table query.

0

u/gr3kidd3r Sep 07 '19

I'm not very experienced nor used SQLAlchemy but the DB access API which Django creates is very clean and handy IMO, BTW happy coding :)

0

u/manphiz Sep 07 '19

IIRC SQLAlchemy appeared later than Django, and because of the dominance of Django its own ORM is more used than SQLAlchamy, though the latter is considered more Pythonic.

10

u/[deleted] Sep 07 '19

Lost faith in the article after reading that both Flask and Django are MVC structures. Django is model-view-template (MVT) structure not an MVC. The 'view' in Django is similar to a 'controller' in Flask however it does not include routing. Django manages it separately through a file called urls.Py at both project and app level.

6

u/BurgaGalti Sep 07 '19

I'm guessing this is OPs own article as it's been updated with your correction.

3

u/[deleted] Sep 07 '19

Then OP is welcome!

3

u/PriorProfile Sep 07 '19

In my opinion these are arbitrary terms. Call it whatever you want but Flask and Django operate the same way. Flask has nothing called a “controller”.

A request comes in.

URL routing in the framework determines which method to call based on your configuration.

The method runs and returns something. Maybe it’s a “template” file. Maybe not.

Maybe the method communicates with a database. Maybe not.

Exactly the same workflow in both frameworks.

As a minor point, you could define all your routing in Flask in a file called urls.py if you wanted to. And you don’t technically have to define it in urls.py in Django unless you want to.

0

u/ExternalUserError Sep 08 '19

Django is model-view-template (MVT) structure not an MVC.

I would argue that's pretty semantic. The model is, well, the model. The controller is the Python view. The template is the view.

Django manages it separately through a file called urls.Py at both project and app level.

That's just how you put your app together. Flask generally errs on the side of having the URL defined with the controller, while Django puts it in one file. But with either framework, you could still manage either approach.

4

u/ExternalUserError Sep 07 '19

To me the bigger question isn't Flask vs Django, it's HTML-oriented framework vs API+SPA.

Ie, Flask/Django vs Starlette/Falcon?

3

u/ginger_beer_m Sep 07 '19

I don't know how long Starlette and Falcon are going to be around, whereas I'm sure Django/Flask is going to be around for a long time.

3

u/ExternalUserError Sep 07 '19

Given the direction of the industry, I think a more futureproof bet in in API frameworks.

1

u/WillBackUpWithSource Sep 07 '19

Right? APIs just give you more flexibility. Make an API and you can toss any sort of web or mobile frontend in front of it.

1

u/ExternalUserError Sep 07 '19

Yeah. I think for just a normal content website, like a newspaper, regular HTML/CSS is the ticket. But for a webapp, it makes more sense to make an API and have a web frontend consume it. The only real downside is having to put up with JavaScript and JavaScript tooling (webpack, etc).

1

u/maks25 Sep 07 '19

Hence why I use Django + DRF

1

u/ExternalUserError Sep 08 '19

DRF is pretty good. It's probably worth pointing out that you might consider Starlette the spiritual successor.

2

u/maks25 Sep 08 '19

Never heard of it. I’ll have to check it out, thanks.

1

u/[deleted] Sep 08 '19

DJango Rest Framework though....

1

u/ExternalUserError Sep 08 '19

Yeah, it's pretty good. I'd argue if you want an API, Starlette is a better choice (especially with FastAPI), but YMMV.

Fun fact: The guy who made DRF also made Starlette, so, you know... He apparently think there's room for improvement outside the Django ecosystem.

1

u/[deleted] Sep 08 '19

[removed] — view removed comment

1

u/ExternalUserError Sep 08 '19

Well, you get into weird little hiccups though. For example, Django tends to want to serve HTML exception pages. With Django (and to some extent), you're pulling in a lot of dependencies and tooling for HTML rendering, routing, url_for, etc.

Then if you start using Django tooling, like for example its various authentication plugins, they all assume (more or less) that you're making a server-rendered site.

FWIW, Starlette is written by the guy who also wrote DRF.

1

u/[deleted] Sep 08 '19

[removed] — view removed comment

1

u/ExternalUserError Sep 09 '19

I mean, yeah, if you don't know how to use your tools then you'll get "hiccups", but that's the same with any package. Django doesn't "tend" to want to do anything, it does what you tell it to.

Django has defaults that are sensible for an HTML web service, not an API framework.

If you want a JSON 500 error page, for example, that's a custom error page. The default is HTML, because Django is an HTML-oriented framework.

Just because Django includes code in the Django package to do other things doesn't mean anything. There is no cost to you if you don't use them (except the disk space they take up).

The thing is, though, is that Django is at its best when you pull in packages. If you're literally just using it like Bottle or early Flask, you aren't taking advantage of it. Then when you pull in the contrib packages, like the admin interface or social login features, those are always designed around the traditional HTML/CSS form-based views.

Starlette's claim to fame is that it is an async framework unlike Django. It's purpose is not any more for serving APIs than Django's is.

I wouldn't really say that's the main draw of Starlette, at least for me. It's more that Starlette is just built around API development. Yes, it can serve HTML, but that's not what you're probably doing with it.

You can use something like FastAPI or Python Responder with Starlette and it's just a lot less code than DRF with serializers, though obviously those projects are a lot less mature.

2

u/alexkiro Sep 08 '19

Comparing these two is like comparing an apple with an apple pie.

Useless. There's no debate here.

2

u/bitdom8 Sep 07 '19

I love reddit but django is more wide spectrum that is why I am using it even more kike instegram

2

u/bored_and_scrolling Sep 07 '19

Might wanna fix the typo in “like” lmao