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 Build a Social Network with Flask Broadcasting Post Form and View

Daniel King
Daniel King
2,317 Points

Purpose of the Flask g object?

Hi all,

I'm having trouble understanding what the purpose is of the Flask g object. In the before_request() function, we assign g.user = current_user. Then, in the post() view, we use the value of g.user in the expression g.user._get_current_object(). My question is, why did we do that instead of just saying current_user._get_current_object()? Similarly, why did we assign g.db = models.DATABASE? Why not just use models.DATABASE throughout?

Any insight as to the purpose of the g object would be appreciated!

1 Answer

Keith Whatling
Keith Whatling
17,759 Points

The g object makes things global... ok so what does that mean, Flask usually runs out of a few .py files, the g object allows you to make things global across your flask app, as in if you have g.ice_cream = 'vanilla' in one file, you can use g.ice_cream in other locations.

It has changed as of flask 0.10. if you look at the social network lesson and i think the second lot of vid's then Kenneth covers it.

Daniel King
Daniel King
2,317 Points

Thanks for that!