Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask Basics Character Builder Starting the Builder

Y B
Y B
14,136 Points

Is there a different type of approach to back ends in Python than Flask, Django etc..?

I've been through the earlier Python courses after which I spent time on the front end courses: Javascript, Jquery, Ajax, CSS 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?

Thanks.

4 Answers

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.

Simple example:

@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. alt text

Y B
Y B
14,136 Points

Great find. Thanks for posting that.

Y B
Y B
14,136 Points

Great find. Thanks for posting that.

You are very welcome.

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.

Good luck.

Y B
Y B
14,136 Points

Thanks for your reply. I'm most of the way now through the flask social app course, and things are definitely clearer. However I'm not sure what the controller is. We use templates, view function and models, but I don't see how the controller fits into that?

Y B
Y B
14,136 Points

Thanks for your reply. I'm most of the way now through the flask social app course, and things are definitely clearer. However I'm not sure what the controller is. We use templates, view function and models, but I don't see how the controller fits into that?

No no no man, flask its absolutely amazing, you can handle all the request you want in a propper way without problems, its just a matter of doing it right and there is a lot of ways to achieve that for example im using angular.js for my website and when i use angular expressions wont work because of jinja2 if you use the render_template('main_html.html') method u have to use jinja2 expressions and this could mess up your plans a little bit but you can always try other solution for example return make_response(open('main_html.html').read()) or something in order to send the html without using jinja2 and when you send request to a route via for example post you can return all your info with json and use it with javascript without a problem, i recomend you to learn about restful services which are extremely easy to achive with flask

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

Y B
Y B
14,136 Points

I didn't fully understand you answer but hopefully I got the jist. I had heard of miguel's tutorial's and books before, so good to know he is highly recommended.

I'm confused about - web api's (or restful api's) is that essentially creating a backend without the html generating part we covered in flask. i.e. is a web api or restful api more in line with what I mentioned above - i.e. all html managed at front end and backend manages http responses and the databased (is this the model people refer to?).
I would have thought that that setup would be the norm. I'm surprised to hear that most of the web frameworks for the backend seem to expect to generate the site html too?

I would love to hear the perspective of some of the mods or other web developers - as this is really confusing me as to what to focus on next. I have no idea if flask or django aren't the best way forward and I should be learning a framework that focuses on the back end only.

Do people normally use Django (and Flask) to generate the whole site - i.e. no JQuery no Javascript, Ajax etc..... Are there limitations with that approach? Is it still interactive in the same way or is it just a fairly static site?

well you could certainly create a whole website with no javascript but thats not how the modern websites work, you gotta understand some web development basic and see that it all a matter of development design, yes you can add a lot of functionality using only request/response 's and buttons and forms but as i said thats not what all of the developers are doing with a website, i recomend you to take a look at this diagram

https://coggle.it/diagram/52e97f8c5a143de239005d1b/56212c4e4c505e0045c0d3bda59b77e5977c2c9bd40f3fd0b451bdcf8da4aa52

and give a try to some modern front end mv* like angular or backbone what do you think?