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 Django Basics Django Templates Templates

What is context? why only self.title?

Hello all, i have two questions: 1- He say all the time "becouse is context" , what is context? what it does? 2- in the model :

class Course(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=255)
    description = models.TextField()

    def __str__(self):
        return self.title

It only return self.title , only title is converted to string? what about the description? is not needed to be returned too? becouse in the template it return both title and description, i am a bit confused. Thank for the help and sorry for my bad english :D

1 Answer

Hi Yen

context in django is a dictionary. So if i wanted to pass name and address to my template, my context would be like below

{'name':'yourname','address':'your address'} 

Then in the template, you would access the variables like so

{{name}}{{address}}  

. In terms of the class returning the title

  # that only happens when the print function is called on the class.
  def __str__(self):
    return self.title

To access the title or description in the template, it would be the same as above, see below

{{course.title}} {{course.description}} 

hope this makes sense .

Great description. Cleared it right up for me!