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 Anstett
5,831 PointsHow does the value of PK, the named regex, get passed to views.courseDetails?
I understand that PK, the named regular expression, does two things. One, "listens" for a match of any numeric value and then "forwards" you to the views.CourseDetails. Two, the single numeric value is then used in the query for course objects (...objects.get(pk=pk).
My question is, where is "pk" passed to the views.py? In the Django documentation, there is an argument which allows you to pass additional arguments to the view function but that is the third parameters and is not used in this example.
Perhaps I missed something that the passed regex can be utilized in the view functions.
1 Answer
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsHi Brain
The pk gets passed to the view from a link tag in your templates. Lets assume you output all the courses on the home page but want to show a detailed description of the course. You would do it like so
{% for course in courses %}
<a href="{% url 'views.course_detail' pk=course.pk %}">{{course.name}}</a>
{% endfor %}
hope this makes sense