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 Using a Model Form

Help: NoReverseMatch using Django 2.0


I need some help. I have a NoReverseMatch message error and I don´t know how to fix it. I guess the cause is that I am using Django 2.0, but don´t know what I have to change

NoReverseMatch at /courses/1/ Reverse for 'create_quiz' with keyword arguments '{'course_pk': ''}' not found. 1 pattern(s) tried: ['courses\/(?P<course_pk>[^/]+)\/create_quiz\/$']

urlpatterns = [
    path('', views.course_list, name="course"),
    path('<course_pk>/<step_pk>/t', views.text_detail, name='text'),
    path('<course_pk>/<step_pk>/q', views.quiz_detail, name='quiz'),
    path('<course_pk>/create_quiz/', views.quiz_create, name='create_quiz'),
    path('<int:pk>/', views.course_detail, name='detail'),

I don´t know how to add different so I post in different comments

def quiz_create(request, course_pk):
    course = get_object_or_404(models.Course, pk=course_pk)
    form = forms.QuizForm()

    if request.method == 'POST':
        form = forms.QuizForm(request.POST)
        if form.is_valid():
            quiz = form.save(commit=False)
            quiz.course = course
            messages.add_message(request, message.SUCCESS, "Quiz added!")
            return HttpResponseRedirect(quiz.get_absolute_url())
        return render(request, 'courses/quiz_form.html', {'form': form, 'course': course})

3 Answers

{% extends "courses/layout.html" %}

{% block title %} New Quiz | {{ course.title }} {{ block.super }}{% endblock %}

{% block breadcrumbs %}
    <li><a href="{% url 'detail' pk=course.pk %}">{{ course.title }}</a></li>
{% endblock %}

{% block content %}
<div class="row colums">
    {{ block.super }}
    <h1> Make a new quiz</h1>
    <form method="POST" actions="">
      {% csrf_token %}
      {{ form.as_p }}
      <input type="submit" class="button" value="save" />
{% endblock %}
{% extends "courses/layout.html" %}
{% load course_extras %}

{% block title %}{{ course.title }}{% endblock %}

{% block content %}
    <div class="row columns">
        {{ block.super }}
            <h1 class="">{{ course.title }}</h1>

            <div class="callout secondary">
                {{ course.description|markdown_to_html }}

                {% for step in steps %}
                        <a href="{{ step.get_absolute_url }}">{{ step.title }}</a>
                    <dd>{{ step.description|markdown_to_html }}</dd>
                {% endfor %}
        {% if user.is_authenticated %}
        <a href="{% url 'create_quiz' course_pk=course.pk.id %}" class="button">New Quiz</a>
        {% endif %}
{% endblock %}

Indentention problem....