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 Welcome to Flask View Args Through URL

Manish Giri
Manish Giri
16,266 Points

Flask terminology

I just started the flask course, and am literally stumped. There were many Flask terms thrown right away, without a detailed explanation. Some of them being - routes, views, controllers.

I could understand only this much: When a request comes in from a user, python looks in the 'app' for a matching 'route'. I'm not sure where 'views' fit in.

I'm now stuck in the challenge task 1: add a new route to hello that accepts a name.

Can someone help me out with a simple understanding of the terminology. I had a better understanding of the MVC-terminology over at the rails course(sigh!).

flask_app.py
from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello():
     return 'Hello Student'

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The classic "MVC" (Models, Views, Controllers) doesn't fit precisely with either the Python Flask or Django worlds. The classic sense "views" are what is displayed to the user, and "controllers" are what examine database data, evaluate forms, and prep data to be displayed in these "views"

Flask and Django, would be better call "MTV" for Models, Templates, and Views. In this scenario, URLs are connected to the view functions through app.route decorators (flask) and the url function (django). The view function prepares the data then renders the Template to be presented to the user.

The view function in Django can also be a Class based view. More on the MTV in a longer post from Django Docs.

To your question of this Challenge: Add a new route to hello() that expects a name argument. The view will need to accept a name argument, too. Step by Step:

# Step 1: Add a new route... this means to create a new '@app.route' statement
@app.route('/')
@app.rount('/????') # Something needed here
def hello():
     return 'Hello Student'

# Step 2: ...that expects a name argument 
@app.route('/')
@app.rount('/<name>') # 'name' argument between < and > 
def hello():
     return 'Hello Student'

# Step 3: The view will need to accept a name argument 
# The URL '/somename' will pass value 'somename' as the 'name' argument
# The 'hello' function needs an input parameter to be able to accept this argument
@app.route('/')
@app.rount('/<name>')
def hello(name): #<-- add 'name' parameter to accept argument from route
     return 'Hello Student'
Josh Keenan
Josh Keenan
20,315 Points

Route: The current address on the site, like '/login' or '/games'. The route '/' is the default page you will be on when you run your app, it's like the index page View: A view is like a function, also called controllers in many other languages and libraries in Python, these are what CONTROL your site, hence the name controller. I prefer controller to view but I recognise them as one and the same. Essentially these get input, if they get input x they then do y.

Do you understand this? It's cool if you don't, don't worry, I'm not the best at explaining stuff..