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 Model Form for Answer

Model Form Answer - my answers don't save!

Whenever I add an answer and click save, the answer doesn't save. I've run through the video a few times, and have double checked my views.py.

Can anyone spot the issue? Also it wont let me attach my code from workspace so had to copy it in.

answer_form.html -

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

{% block title %}New Answer| {{ question.prompt }} | {{ question.quiz.course.title }} {{ block.super }}{% endblock %}

{% block breadcrumbs %} <li><a href="{% url 'courses:detail' pk=question.quiz.course.pk %}">{{ question.quiz.course.title }}</a></li> <li><a href="{% url 'courses:quiz' course_pk=question.quiz.course.pk step_pk=question.quiz.pk %}">{{ question.quiz.title }}</a></li> <li>{{ question.prompt }}</li> {% endblock %}

{% block content %} <div class="row columns"> {{ block.super }} <h1>Add Answer</h1> <form method="POST" action=""> {% csrf_token %} {{ form.as_p }} <input type="submit" class="button" value="Save"> </form> </div> {% endblock %}

answer form part of views.py -

@login_required def answer_form(request, question_pk): question =get_object_or_404(models.Question, pk=question_pk)

form = forms.AnswerForm()

if request.method == 'POST':
    form = forms.AnswerForm(request.POST)
    if form.is_valid:
        answer = form.save(commit=False)
        answer.question = question
        answer.save()
        messages.success(request, "Answer added")
        return HttpResponseRedirect(question.get_absolute_url())
return render(request, "courses/answer_form.html", {
    'question':question,
    'form':form
})

Thanks in advance!

I have since corrected "if form.is_valid:" to "if form.is_valid():" and it still wont save :(