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

what are kwargs , what do we use them for and when , and do we always need to use context to pass data from models?

Offen during tutorials in the internet there is info about kwargs , what are we use them for and when , also there is a context 'variable' , do we need to use it always to pass data from models to templates , can we use diffrent way to pass data from models to templates ?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Good question! Arguments are passed into functions and matched to parameters in the function definition. There are two types of arguments:

  • positional arguments: those without keywords, also called "args". The shorthand *args says "assign positional arguments to the list args".
  • keyword arguments: those with keywords, also called "kwargs". These are always listed after any "args". The shorthand **kwargs says "assign keyword arguments to the dict kwargs.

When data is passed from models to views, there is always a context dict passed to hold all of the request information in addition to the model variables. Depending on the syntax you use, it is also possible to pass values to the templates outside of the context dict by using assigning the value to a keyword in the template call within the view. This keyword/value pair may also be referred to as a "kwarg" (that is, a "keyword argument"). Passing values outside of the context is not standard and may be confusing to future programmers reading the code.

Post back if you need more help. Good Luck!