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 trialBrian Hudson
14,785 PointsWhat does 'article' refer to in the code challenges? (Django Class-Based Views)
In the Django class-based views code challenges a few of the solutions require code like this:
`{{ article.title }}'
or
'{{ article.author }}`
I don't know if I'm just having a brain cramp or if I'm missing a fundamental django concept but I do not know what 'article' is referring to? I know the model name in these challenges is Article. And the field names appear to be 'title', 'author', 'body' and 'published'. I just don't know what this lower-cased 'article' is. It appears to be an instance, but where is it named as such?
Brian Hudson
14,785 PointsBtw Stuart if you answer this as an "answer" instead of as a comment I'll be able to mark it as Best Answer ;)
2 Answers
Stuart McIntosh
Python Web Development Techdegree Graduate 22,874 Pointshi there, look in the views.py. I would believe the article an instance of the get_object_404 call on the Model Article and is then rendered along with the template. for instance it may look like this :
def article_detail(request, pk):
article = get_object_or_404(Article, pk=pk)
return render(request, 'articles/article_detail.html',
{'article' : article})
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Brian,
To address your specific question as to where the lower-cased article
comes from. This is a feature that Django provides in DetailViews and ListViews where it knows that the associated Model is Article
so it can automatically generate context variable names (<lowercased_model_name>_list
for ListViews, <lowercased_model_name>
for DetailViews).
Kenneth did briefly mention it in the preceding video where he talked about the Team
model receiving an auto-generated team_list
context variable, but it was pretty quick so you would be forgiven for missing it.
For more detail, see Making Friendly Template Contexts in the docs.
Hope that helps.
Cheers,
Alex
Brian Hudson
14,785 PointsBrian Hudson
14,785 PointsThanks Stuart. You're probably right that 'article' is defined as a context variable but there's no way for me to view the actual files the code challenges are referring to. The project in the workspaces is completely different than the one in the code challenges. At any rate, your answer is helpful, I'm thinking you're correct, thanks!