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 Forms Model Forms Edit an Instance

Jimmy Holway
Jimmy Holway
10,703 Points

TypeError at /courses/PK/edit_quiz/COURSE_ID

This is the error I get when clicking the 'EDIT' button we just created based on 'Edit an Instance' video.

TypeError at /courses/3/edit-quiz/17 quiz_detail() got an unexpected keyword argument 'quiz_pk'

I've been poring over my code and I can't figure out what's throwing this error. Here is the model, URL pattern, and both view functions attributed to this error:

# The edit button block

    {% if user.is_authenticated %}
      <div class="ui divider"></div>
      <a class="ui button" href="{% url 'courses:edit_quiz' course_pk=step.course.pk quiz_pk=step.pk %}">Edit</a>
    {% endif %}
# URL pattern
url(r'(?P<courses_pk>\d+)/edit-quiz/(?P<quiz_pk>\d+)$', views.quiz_detail, name='edit_quiz'),

# Quiz Model
class Quiz(Step):
    total_questions = models.IntegerField(default='4')

    def get_absolute_url(self):
        return reverse('courses:quiz', kwargs={
                       'course_pk': self.course_id,
                       'step_pk': self.id
                       })

    class Meta:
        verbose_name_plural = "Quizzes"

# quiz_detail() view
def quiz_detail(request, course_pk, step_pk):
    course = get_object_or_404(models.Course, pk=course_pk)
    step = get_object_or_404(models.Quiz, course_id=course_pk, pk=step_pk)
    return render(request, 'courses/quiz_detail.html', {
                           'course': course,
                           'step': step,
    })

# quiz_edit() 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
    })

1 Answer

Jimmy Holway
Jimmy Holway
10,703 Points

I called quiz_detail instead of quiz_edit in my urlpattern

url(r'(?P<courses_pk>\d+)/edit-quiz/(?P<quiz_pk>\d+)$', views.quiz_detail, name='edit_quiz'),

(╯°□°)╯︵ ┻━┻