Django vs Flask

Video in TIB AV-Portal: Django vs Flask

Formal Metadata

Django vs Flask
Title of Series
Part Number
Number of Parts
CC Attribution - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
Release Date

Content Metadata

Subject Area
When it comes to web development in Python, Django isn’t the only game in town. Flask is a scrappy young framework that takes a very different approach – whereas Django ships with tons of features ready and eager to be used, Flask provides a bare minimum feature-set with rock-solid extensibility and incredible flexibility. This talk, given by a web developer who has experience with both frameworks, takes a good look at the pros and cons for both Flask and Django. If you’ve ever felt that Django is too hard to learn, or too inflexible, this is the talk for you! Outline What is Flask? Historical background Django vs Flask Ideological differences Example “hello, world” websites Database support Django ORM vs SQLAlchemy, Peewee, etc… Non-relational databases User model django.contrib.auth vs Flask-Login, Flask-Principle Django Admin vs Flask-Admin Django apps vs Flask Blueprints Django REST Framework & comparisons to Flask Which is better? Depends on your use-case.
Slide rule Group action Link (knot theory) Bit
Software developer Web-Designer
Web 2.0 Pi Closed set Software framework Open set
Server (computing) Voting Computer animation Computer file MIDI Authorization Code Software framework Product (business) Library (computing)
Repository (publishing) Gender MIDI Repository (publishing) Measurement Social class
Scripting language Server (computing) Computer virus Computer file Code Software developer Web browser Line (geometry) Cartesian coordinate system Integrated development environment Root Personal digital assistant String (computer science) Website Contrast (vision) Local ring
Mobile app Regulärer Ausdruck <Textverarbeitung> Computer file Divisor Model theory System administrator View (database) Multiplication sign Set (mathematics) Content (media) Uniform resource locator Data model Regular graph Endliche Modelltheorie Pairwise comparison Curve Dependent and independent variables Computer virus View (database) Server (computing) Projective plane Debugger Model theory Data storage device Database Regulärer Ausdruck <Textverarbeitung> Cartesian coordinate system Data model Web-Designer Blog
Filter <Stochastik> Standard deviation Information Model theory Database Instance (computer science) Perspective (visual) Web 2.0 Category of being Data model Different (Kate Ryan album) Object (grammar) Blog Software framework Object (grammar) Quicksort Fiber bundle
Installation art Sequel Computer file Model theory Debugger Orientation (vector space) Connectivity (graph theory) Modal logic Coma Berenices Uniform resource locator Fluid statics Methodenbank Software framework Software testing Configuration space Extension (kinesiology) Exception handling Social class Information Relational database Gender Database Directory service Cartesian coordinate system Connected space Subject indexing Data model Error message Personal digital assistant Function (mathematics) Order (biology) Website Configuration space Object (grammar) Figurate number Extension (kinesiology)
Slide rule Sequel Information Model theory Gender Direction (geometry) Multiplication sign Bit Database Instance (computer science) Content (media) Power (physics) Data model Data model Query language Blog Order (biology) Query language Object-relational mapping Quicksort Object (grammar) Object-relational mapping Alpha (investment) Social class
Model theory System administrator Database Generic programming Login Vector potential Data model User profile Computer configuration Different (Kate Ryan album) Information Endliche Modelltheorie Extension (kinesiology) Pairwise comparison Google App Engine Physical system Installation art Axiom of choice Information Relational database Building Gender Data storage device Database Web application Data model Interface (computing) Software framework Quicksort Object (grammar) Physical system Extension (kinesiology)
Web page Ocean current Home page Boolean algebra Model theory View (database) Password Price index Login Pointer (computer programming) String (computer science) Mixed reality Software framework Integer Routing Dean number Social class Exception handling
Default (computer science) Standard deviation Gender System administrator Principal ideal Model theory Electronic mailing list Maxima and minima Similarity (geometry) Login Database Flow separation Front and back ends Similarity (geometry) Bootstrap aggregating Computer animation Different (Kate Ryan album) Quicksort Extension (kinesiology) Physical system Physical system Social class Extension (kinesiology)
Email Mobile app Code System administrator Multiplication sign Amsterdam Ordnance Datum Image registration Data model Hooking Different (Kate Ryan album) Logic Software framework Endliche Modelltheorie Extension (kinesiology) Pairwise comparison Information security Physical system Mobile app Curve Projective plane Code Login Image registration Computer animation Logic Software framework Information security Form (programming) Task (computing) Extension (kinesiology)
Mobile app Mobile app Group action Computer virus Information View (database) Model theory Electronic mailing list 1 (number) Cartesian coordinate system Variable (mathematics) Human migration Goodness of fit Computer animation Different (Kate Ryan album) Logic Website Contrast (vision) Quicksort
Meta element Serial port INTEGRAL Code Model theory View (database) Methodenbank Function (mathematics) Data model Methodenbank Different (Kate Ryan album) Core dump Software framework Endliche Modelltheorie Extension (kinesiology) Pairwise comparison Abstraction Building Web page Complex (psychology) Bit Representational state transfer Web application Curvature Software framework Website Software testing Pattern language Quicksort Web page Ocean current Point (geometry) Sequel Authentication Password Login Field (computer science) Data structure Contrast (vision) Dean number Form (programming) Multiplication Information Projective plane Line (geometry) Cartesian coordinate system Word Computer animation Password Abstraction Extension (kinesiology)
Point (geometry) Axiom of choice Connectivity (graph theory) Multiplication sign Set (mathematics) Database Template (C++) Web 2.0 Optical disc drive Programmer (hardware) Bit rate Different (Kate Ryan album) Fiber bundle Software framework Contrast (vision) Extension (kinesiology) Pairwise comparison Error message Form (programming) Physical system Covering space Raw image format Axiom of choice Relational database Gender Projective plane Bit Cartesian coordinate system Similarity (geometry) Component-based software engineering Function (mathematics) Software framework Object-relational mapping Quicksort Extension (kinesiology)
Web page Complex (psychology) Functional (mathematics) Computer file Code View (database) Multiplication sign Parameter (computer programming) Web 2.0 Heegaard splitting Latent heat Bit rate Term (mathematics) Different (Kate Ryan album) Software framework Router (computing) Vulnerability (computing) Task (computing) Physical system Form (programming) Scaling (geometry) Slide rule Gender Model theory Coma Berenices Bit Cartesian coordinate system Logic Personal digital assistant Order (biology) Quicksort
and huh long and
no and
no and I'm so if you're having trouble viewing the slides particularly on the side of the room action have a link to then it's bit leading con last so you can check it on computer and follow along and jump ahead you achieve a so has Brian said hi I'm
DB I'm a freelance web developer corporate trainer I do lots of stuff on the side so if you're if you're a company like what to say or interested the having implements jagoff last for you but not so i'm you talk about
Python and went and if you're here Jenga common you're already familiar with Django of course
but there's lots of other Python webdev frameworks out there so gender was actually influenced by several earlier frameworks legs open and close and turn has inspired lots of other frameworks that came after it
like pyramid TurboGears cherry pie bottle and Web 2 pi but the 1 that I'm here to talk about is called
flask now flask is a micro framework that has some surprising
beginnings actually started as an April Fool's joke if you can believe it but the author of this framework had written to other frameworks called votes which is like a server framework and ginger 2 which is a templating library anything it would be kind of fun to take these 2 libraries but and in together into a zip file and then write this denied framework that when we ran it would just automatically unzip these libraries larger computer and run them and thought everyone would just laugh and instead everyone said this is fantastic we need to actually use this in production so he turned it into an actual production ready framework that is it actually gotten surprisingly popular if you look on give up
today you will see that if you list all of the Python repositories GenGO and plaster both up there and blast actually has more stars than does so if that's your measure popularity than flask is more popular so you might be asking
yourself why is fast so popular is actually better than gender and the answer as you might imagine is it depends J. goes large class
this small both of them are good there were from the community we love each other the so let me give you some examples to introduce what flask is and how to use it I'm assuming that you have some familiarity some familiarity with Django but that you know little to nothing about about flask so when we started programming we always start with Hello World
here is the hello world application in flask as you can see it is literally a 5 lines of code and you run it by calling flask run and setting an environment variable to let the flask command line script and know where to find your application so in this case I've taken these 5 lines of code and put in a file called hello that pi so just a flask Abigael's hello up flask run and that will start a local development server on port 5 thousand and if you visit the root about website you'll see the string Hello World in your browser it is really that easier but by contrast if you want to do Hello world in Django you start by installing
Django many django admin stop project project you don't your project you make your hello app which then of course you have to put that into the seven-step that I then you write your hello
view which requires importing this HTTP response thing and returning it and then you have to deal with your else that pi which means writing regular expressions which is always a pain especially for someone who is new to programming and doesn't necessarily know how to do regular expressions so I've actually found when talking with people who are new to the web development in Python agenda was more intimidating to beginners and plasters gender has a steeper learning curve because you have to go over settings and regular expressions you have to understand the concepts of projects versus applications flask is much simpler to get started you can actually have your entire project in a single file and that works really well the so by that account fast winds out with their simplicity factor for getting started but projects are generally more complicated than Hello world typically wanna store some data in the database modify it and render that data on on the front end the so how do you do data modeling well let's start with Django this time well and Django we're gonna use the GenGO or and so you do from DB import models in him defining a very basic blog post model this is co that you've probably seen before but to manipulate the data
you just create an instance of that assign information on properties and do of object that save and then you can query the database by doing blog post of objects and you can do filters and you can do selects and all sorts of standard things that you can do with database queries so how do you do the same sort of thing and flask all
the answers you don't so flask actually doesn't have data models now you're looking at this and you're wondering how can that be how can they web framework not have a data model how can the web from that's apparently more popular than Django not have a data model for the answer is that jet is that flask has a very different perspective and ideology from GenGO GenGO bundles everything altogether and flask is much more modular so
here's an example of how you might handle data modeling in Django versus flask you can see that the GenGO orient is built into gender itself so you install GenGO you got flask doesn't have that but flask has connections to other Python modules in the Python Package Index that will do data modeling for you for example the sequel alchemy or M is a very popular 1 if you wanna do not relational databases Mongo DB you can use Mungo engine is a smaller framework called kiwi that's been getting some attention lately so the idea is that flask is
very very minimalist it has almost nothing in there it has only the bare necessities for what you need in order to do a simple static web site essentially it includes templating Ural rounding error handling and the butter that is all however it is designed to be incredibly extensible so you can plug and play and shoes which components that you want to plug into your website and make it work exactly the way that you want so let me give you an example that shows you how you might do data modeling using sequel alchemy and flask so there's an extension called flask seek welcoming which was designed to clean we integrate these 2 different modules so you just install stalls to sequela me itself as a dependency and then you do some basic set up to configure something import that extension and I'm going to configure it onto my flask applications and as you can see the fast application also has this dot com figure object where you can set information that your application or extensions can read and use so in this case I might say that I want my my database statistical I database that uses the test that DB file under the temp directory whatever the thing that's really important here is that you notice that URIs importing the sequel alchemy class and you're creating a variable called DB that you you get by wrapping your application sequel so now that you have this in DB object you can do some very familiar things with so here I'm
defining a data model a blog posts data model and you can see that looks very familiar to the same sort of thing that you would do with the gender or and in fact all take the previous example alpha on the same slide so
you can compare and contrast they're not identical but they're very similar to each other and if you've used the GenGO or and you can use equal alchemy in almost the same way you can also manipulate data in very similar fashion so so you can create an instance of a blog post of that an instance of a blog post class assigned information to it and then instead of calling that save on it you have to add it to the database session and commits the session it's the same basic concept sequel alchemy is just making you sort of be a little bit more explicit what how this database direction works and you can query data in the same way so you do . query instead about objects and you can deduct filter order filter by and there is a lot of the same basic things that you can do in and you can do the same sort of thing in the book in fact I'm of the opinion that the problem is a more powerful object relational mapper avenging or but that's another topic for another time so the the
summer compares the so to talk about the animals here on higher-level Janos data models are easier to get started because the built so you don't need to import and install anything else to get started however flask allows more flexibility to choose where everyone use GenGO assumes that you're going to use a relational database flask you can use whatever you want you can use Monge you can use Google App Engine status store you can use whatever it doesn't matter but of course the more options you have the more flexibility you have the more chance you have to screw something up so it's a trade off let's keep going most web applications have users and they also have an admin to be able to view information in your database and modified so how do these compare can you do this sort of thing with last well with gender we
have you're familiar Jagoda contraband off is built in it's easy I'm sure you're all rather familiar with it if you need extra mile if you need extra information producers you can spot that the user model at a little complicated you can also make a user profile to attached to it but also a little complicated but works pretty well I with the admin you have gender a country that and also built an easy very customizable and there's a lot of documentation of there with a fine grained permission system so that different admin users can get access to different objects to and Mr. so how we do and flask well as you might imagine you don't have users built because you don't even have a data model built it but there is a very popular flask extension called last login which is generic and works with just about any data model including signal alchemy you like like so here's an example of how you might
you that I'm going to define a user class and you can see I'm importing this user mix from from class log in a gives a couple of extra little superpowers so that we have some standard usage that you can use across your framework the regardless of whether you're using cecropin your mother or whatever so for example In my routs I might say if current user . is not the current user something that provided by + login is a pointer basically to whatever users logged in and the is anonymous things provided by the user makes another showed you early so here's a simple view where I'm saying if you're anonymous just render the splash page otherwise show the user homepage off last login will also give you a login
required decorator which you've probably seen from general as well so it's just a decorator you apply the review if the user tries to access the view and are not logged in all get a 403 Forbidden exceptions again very similar to GenGO but the idea is that you build this piece by piece
with user permissions you can also use the fast principle extension that has a very similar fine grained permission system the same way that gender general user Django contrib off models do so the idea is if you don't want a permission system you don't need to have gender users have that Appleton by default and you have to sort of deal that whether you want or not with fast you can decide if you want a better and the admin as you might expect there's a flask Adnan extension as well so you wanna use that you install it you set up and you decide which the new 1 use as a couple of things built and based on bootstrap or you can write your own it works with several different database backends including sickle alchemy and ongoing peewee and it's designed to work with or without any sort of other date any user extension you want so it's very common to have a working class laughin about pretty fast principle does not require some screenshots
of how it looks here is the list used to conceal their users here's how you enter the user as you can see it's all standard bootstrap so it's pretty familiar and pretty powerful now there's a lot of different extensions of have just gone over and having a user in admin system is pretty standard so there's actually an
extension called last security which all it does is it takes about 5 or 6 different flask extensions and bundle them all together into 1 package so they're already designed to hook up together properly so you can just installs 1 extension and then you got the users to permissions your admin it's all there it's great and of course it work to seek welcome model engine or the week so you've got a lot of flexibility so again Django has a user for a user framework an admin built-in and they work very well but they're not maybe they're not as flexible as personally I would like but for a lot of people they were great flask requires a lot of extensions working together in concert which makes for a steeper learning curve but it means that you can define your user model in your permission system to work exactly the way that you want so it's this the question do you want off the shelf or do you want extensive customization there's also the idea of reusable
apps so gender has got this nailed with the whole jego apps system you have to have a nap every time you create a project all code related to 1 concept was in 1 place like for example you might have all the registration logic in 1 place how does that compare so what gender you install your
thing and you get it set up in the installed apps lists in the sentence are you have a general packages website available which is fantastic and shows you a lot of good information that is available but is a little hard to figure out which packages you actually want which ones are maintained a higher quality and so on and of course if you're writing your own applications very tempting to stick everything into 1 app rather than organizing into several it some complicated to figure out how you wanna move all those pieces around by contrast blue flask has
something called blueprints which are not quite the same thing but they're pretty similar it's a way of organizing the views in your application so that you can again group logic together into similar places but it doesn't require that you move models in 2 different places it doesn't require anything about migrations in different places it's basically just view so it's much smaller and much more light which might be a good thing or a bad thing depending on how on how you look at it it's also very familiar syntax so when we go back to our basic hello world that we had before and I will take this application and all transform it into a blueprint just like this you can see the only things I had to change was turned the AP variable into a blueprint variable and I can still do the same basic Rao decorator on top and now once I have this hello underscore BP blueprint I can attach it to existing applications just by importing it from the fire where have a defined article Apr . register blueprint of the blueprint of define so it makes it much easier to take an application that was originally defined as 1 monolithic application and separated out into several different blueprints although you might do the same sort of thing which I apps
so to compare gender out a more comprehensive there's a lot more of them out there especially if you check the Jacob packages websites but also more complex and sometimes it's hard to refactor your application into collapse by contrast fast blueprints a simpler and easier to integrate the project but maybe they don't provide power that you're looking for again it's it's sort of a subjective thing another thing a lot of websites need is API as the increasing common for better for web applications and they have different user patterns compared to HTML web pages so how to to compare well when you're dealing with Django you want using the rest framework it's great I'm sure you've all heard tons and tons of praise from this framework over the past few days it's multilayered extraction if moltaí layered abstraction so you can choose which where you wanna go with it has tons of documentation and it works great so what about what flask will as you might imagine you wanna use multiple DOF extensions working together to provide the experience there looking for of with generous framework typically the thing that sort of forms the core of how your API structured is the serializer and in flask you would probably want to use the marshmallow module which is as you might imagine a serializer framework and the ecosystem has integrations of flask with sequel alchemy would Mongo engine with a whole bunch of other things so no matter what sort of structure you decided for your webapp flat marshmallow will work properly with so let me give you an example this is a fair amount
of code but this is of an application written flask that returns adjacent based API endpoints to return information about the currently logged in user now annotate this a little bit you can see at the start I'm importing whole bunch of stuff and then I'm going to initialize the flask marshmallow extension and save it into a variable called and that I'm going to define my schema which is basically the serializer of generous framework uses the word serializer marshmallow uses the word schema is the same basic thing and you can see I'm telling it's just find the fields defined on the user model and exclude password field because we don't want that being displayed on API and then in the actual API view I can just say initialize this user schema and take the current user and output it has Jason and I also have the login required decorated that I talked about earlier which will make sure that you can only access this API and point if you are currently logged in so this is a one page this is maybe 15 lines of code and have a basic API set up in such a way that you the understand every single piece of the puzzle of how to put together so gender versus fast
when it comes to API as generous framework is amazing I really wish the flask had something else as well put together and as clean and as abstracted as generous framework we don't have that yet maybe someone will put that together the same when the people do that for fast security but as it is you can still use all the extensions that you want to put together something just as powerful as the error if not more so and you have more flexibility as well because you're not constrained to the constraints the gender puts upon you can use a non relational database you want to you can use any sort of different components that you want to mix and application to make it taste exactly which you want the so the question might be asking yourself is which 1 do I choose and of course it's up to you it depends on the project but let me give you a couple of Freeport points so that you have an idea of which to go for you generally want choose GenGO when
you're happy with all the choices the gender makes for a so I've been talking about how gender restricts you 2 using a relational database maybe you like using a relational database if that's the case go for it but jingo makes use gender templates although that's change recently stop at templating systems but generally you wanna go with the things agenda provides for you the more you do that the happier you'll be if you're not doing anything to unusual gentle work great for you because the more the stratified the framework the more pain your experience and also sort of sets up a whole bunch different things for you can you can pick on covers and you can learn how the or em works and how the templating system works and how these different pieces fit together but you don't necessarily have to by contrast you might wanna choose flask when you disagree with 1 Agenda stresses anyone do things differently if you wanna use outcome instead of the general form for example or if you have unusual requirements like using a relational database were doing something else that seems a little bit odd or unusual for web framework django will find you a little on this flask will say yes to which you what rate or it's great to use fast when you're doing a hobbyist side project and maybe you're less concerned with having a working project and more concerned with understanding all the different layers and how they work together it's great for really making you learn how the pieces fit together ever make you a better programmer by having that understand so about time
but that's all I have to say does anyone have any questions I have a little bit at times 2 questions and I will also say I'm sure there people here with a lot of opinions so I'm going to be out in the lobby to host an argument session after the stock so I built a flask application last weekend on and coming from an object-oriented world I was like worried you know I could go this in in 1 file that I really was put my router out and I wanna split my models l and I wanna have my talking like and I really couldn't find anywhere on the web that actually tells you how to do that there's a lot of things that say here's how do this in page here and there's lots of things that sort of see the really complex sort of like here's how you put all this together there's nothing really says like here's how you put your router in just 2 different file the there's to work but I feel like I was doing something wrong you have like how you actually separated concerns so flasks greatest strength is its flexibility and that is also its greatest weakness so you can separate your file in many different ways into you know whatever names you want in terms of the actual functionality actual step-by-step way of doing that you just take your code copy pasted into different file and then in your main file you imports what you copy pasted from someplace else so it's just using Python's import system of something specific to flask in terms of which files you pull those out into how you name them where you move your code around that is a subjective thing and that's gonna depend on what you're interested the couple of tutorials out there and you can follow them if you want but ultimately it's up to you in through the top and he didn't hear choosing a when applied to track question about that I'm trying to keep my question the form question and its you please say more about you're not doing anything unusual because they're doing that little bit leading you might start out doing something unusual that you grow in scale and try new things you might end up doing something you usual need is a little more rally had my there that is very true I I I have to admit I don't have a specific use case in mind for what I'm thinking of but there are people who for example might want to use a web framework in a way that a web framework was never really designed to be used for like maybe using it to do a task you application which I've seen people try to do I've seen people use tried and use a web framework in order to just have a test run on the command line so sort of like you might have rate managed up high and people will try to get some logic into a view or something like that all these are probably bad ideas and gender will try to steer you away from that flask will say yeah this is a very small very flexible framework you can use me the way that you want so again it's a question of how do you want framework pushing you in certain directions or do you want a framework to say yes you know I didn't think that you want to do it that way but if that's really the way that you wanna do it go for it so I guess it it really depends on a specific use-case inside have anything more specific to say that the unfortunately removed time for questions the down to continue in the hallway too and until then listen to the ground truth didn't do much
her left so if we were