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

BRIAN WEBER
BRIAN WEBER
21,570 Points

Edit Button not displaying on "quiz_detail.html"

For some reason, the "Edit" button is not showing up on my "quiz_detail.html" page. I am able to navigate and edit a quiz successfully by manually inputting the url. Here is my code for quiz_detail.html, views.py and urls.py. Any help would be appreciated! I have verified that I am logged into the admin.

"quiz_detail.html"

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

{% block title %}{{ step.title }} | {{ step.course.title }} {{ block.super }}{% endblock %}

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

{% block content %}
    <div class="row columns">
        <article>
            {{ block.super }}
            <h1>{{ step.title }}</h1>
            Quiz questions here
        </article>
        {% if user.is_authenticated %}
        <hr>
        <a href="{% url 'courses:edit_quiz' course_pk=step.course.pk quiz_pk=step.pk %}" class="button">Edit</a>
        {% endif %}
    </div>
{% endblock %}

"views.py"

from itertools import chain

from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render

from . import forms
from . import models


def course_list(request):
    courses = models.Course.objects.all()
    return render(request, 'courses/course_list.html', {'courses': courses})


def course_detail(request, pk):
    course = get_object_or_404(models.Course, pk=pk)
    steps = sorted(chain(course.text_set.all(), course.quiz_set.all()),
                  key=lambda step: step.order)
    return render(request, 'courses/course_detail.html', {
            'course': course,
            'steps': steps,
        })


def text_detail(request, course_pk, step_pk):
    step = get_object_or_404(models.Text, course_id=course_pk, pk=step_pk)
    return render(request, 'courses/step_detail.html', {'step': step})


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


@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, 
                                 messages.SUCCESS, 
                                 "Quiz added!"
                                )
            return HttpResponseRedirect(quiz.get_absolute_url())
    return render(request, 'courses/quiz_form.html', {'form': form, 'course': course})


@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)

    if request.method == 'POST':
        form = forms.QuizForm(instance=quiz, data=request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, "Updated {}".format(form.cleaned_data['title']))
            return HttpResponseRedirect(quiz.get_absolute_url())
    return render(request, 'courses/quiz_form.html', {'form': form, 'course': quiz.course})

"urls.py"

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.course_list, name='list'),
    url(r'(?P<course_pk>\d+)/t(?P<step_pk>\d+)/$', views.text_detail, name='text'),
    url(r'(?P<course_pk>\d+)/q(?P<step_pk>\d+)/$', views.quiz_detail, name='quiz'),
    url(r'(?P<course_pk>\d+)/create_quiz/$', views.quiz_create, name='create_quiz'),
    url(r'(?P<course_pk>\d+)/edit_quiz/(?P<quiz_pk>\d+)/$', views.quiz_edit, name='edit_quiz'),
    url(r'(?P<pk>\d+)/$', views.course_detail, name='detail'),
]

Thanks,

Brian

3 Answers

BRIAN WEBER
BRIAN WEBER
21,570 Points

I just realized my mistake below:

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

The template I was returning was for step_detail.html so I changed the return line to the following:

return render(request, 'courses/quiz_detail.html', {'step': step})

Thank you!

Seph Cordovano
Seph Cordovano
17,400 Points

+1 same problem here, thank you!

I'm happy you posted this. I made the same mistake haha

Modou Sawo
Modou Sawo
13,141 Points

I think Kenneth might have made that error, for I ran into the same problem; thanks!