Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Django Forms Model Forms Using a Model Form

Help: NoReverseMatch using Django 2.0

Hi,

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\/$']

urls.py
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

views.py
@login_required
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
            quiz.save()
            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

quiz_form.html
{% 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" />
    </form>
</div>
{% endblock %}
course_detail.html
{% extends "courses/layout.html" %}
{% load course_extras %}

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

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

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

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

Indentention problem....