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

I want to understand primary keys (pk) and ids more. Help please?

I understand their functionality a bit, but sometimes get lost when they get so long, like in this anchor tag:

<a href="{% url 'courses:edit_quiz' course_pk=step.course.pk quiz_pk=step.pk %}"></a>

It's for this view:

@login_required
def quiz_edit(request, course_pk, quiz_pk):
    quiz = get_object_or_404(models.Quiz, pk=quiz_pk, course_id=course_pk)
    form = forms.QuizForm(instance=quiz)
    return render(request, 'courses/quiz_form.html', {'form': form, 'course': quiz.course})

2 Answers

What will you like to understand about (pk) here you're using a template to send values to the view function with the parameters specified and returning a html to the renderer and passing a form instance and accessing the course property on the quiz instance.

Thank you.

href="{% url 'courses:edit_quiz' course_pk=step.course.pk quiz_pk=step.pk %}"

So for instance in this href above >> The admin made it easier for me to digest. So just like how I would select the Course for a new quiz I'm hypothetically creating/editing in the admin, I select the course name related to that quiz. ▼▼

alt text

href="{% url 'courses:edit_quiz' course_pk=step.course.pk quiz_pk=step.pk %}"

So I'm saying in this href: go to the courses:edit_quiz url (i.e. I'm in admin and I clicked on a question), and then once I'm there: I want you to access the quiz pertaining to that question). Is it sort of like that?

alt text

Yeah each course has a quizzes associated to it depending on the step so each step has related courses and each course a quiz all using the primary key of the selected value.

href="{% url 'courses:edit_quiz' course_pk=step.course.pk quiz_pk=step.pk %}"