Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Y B14,136 Points
Is there a different type of approach to back ends in Python than Flask, Django etc..?
After doing the other courses I was expecting the backend to be quite different to what I'm seeing in the Flask course.
In the how to build a website and ajax courses we sent off data in forms via an http request (either get or post) and it was mentioned that the <input> tag should have a name class so this is more easily processed via the backend.
I was then expecting on the backend that we would be capturing those http requests, dealing with the (JSON) data sent in them and via ORM reading and writing this to a database. (and if needed using python or the database to analyse and process the data). The, generating http responses to the front end with data from the database etc..
However with Flask we seem to be focused on building HTML with it. We also seem to be constricted in how we might have built our front end site and now have to put it in these folders as specified by flask (I expected the back end to leave the front end structure untouched). I think Flask is doing more than I expected and is essentially also doing more of the front end job. (Which is fine - just wondering if there is another way)
ie. Is it common to have a backend more in line with as I described(i.e. doesn't generate html) and what frameworks do people use for this?
I am currently on the part where this comment was left and therefor you are ahead of me when it comes to Flask. However the MVC is the same for almost all framework, its just some of them give things other names. Here is a great link I found you may want to read.
The controller would be the functions that take in value and do some sort of logic on them.
@app.route("/welcome") def welcome(): return "Hey there Y B"
So, the user requests www.exaple.com/welcome with a GET request in this case, the def welcome(): function is the controller allocated to handle that request, and all that function does is return a simple string message of "Hey there Y B" to the user
If you're like me then a picture is a lot quicker.
Flask, like all frameworks(the ones I know) use the MVC architecture. Look into that as it is a must for any developer, especially a web developer. The short explanation, the V in MVC is the VIEW, html in your case. This is what the user will see, the M stands for MODEL, this is some sort of storage or values. Your VIEW will obviously want to display some of that data but can't get access to them. For example, you can't get data from a database like MySQL with HTML code, thats where the CONTROLLER comes into play. It is the intermediary between the two.
In-depth Example: 1) User goes to website and is presented with the log in page - (VIEW) 2) Once the data is entered, Flask gets that data - (CONTROLLER) 3) Flask then queries the database(MODEL) - (CONTROLLER) 4) Flask get the data back from the database and see if the credentials entered in the html (VIEW) match what is in the database (MODEL) - (CONTROLLER) 5) If the credentials are a match, then the controller presents the user with the Welcome back page (VIEW)
Now, like you said in reply to Abdul hamid Achik Lopez, APIs also use the MVC architecture but do not have the frontend component. Not entirely true as the recipient may then display the data it received back from the CONTROLLER on your phones screen which would be the VIEW.
Pinterest is built using Django, as is Instagram. Lets take Pinterest for this example, the whole website is built on Django, that means Django is in charge of dealing with the MVC architecture, it deals with user login, registration, searched etc. When the data is displayed on the screen (html) / frontend then the skill set you currently have comes into play, you can change the look of the website, change the colours, make it resize depending if it is on a phone or a computer, Django doesn't deal with that. Pinterest also has apps on iPhone and Android, for that the phones would be the VIEWS, the user interacts with the device, then sends the changes to Django to save them on the database etc or fetch data from the database. Once Django has the data you asked for, it the sends the data back to your phone in a format such as JSON, at which point you would display on the phone with some frontend prettiness.
I know its a long answer but there was a lot more I could have written, lol. So to finalise, continue learning Flask, if you don't like it then move to another framework like Laravel which is in PHP but no matter what you move to, you will need to learn the MVC structure.
If you have any specific questions please don't hesitate to ask.
additional info: i strongly recommend you to buy the web dev book with flask writted by miguel grinberg its just absolutely amazing and well explained.
Edit: added aditional info
and give a try to some modern front end mv* like angular or backbone what do you think?