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

Hasan Ahmad
Hasan Ahmad
6,727 Points

AttributeError

I am following along with the video but for some reason, I get an AttributeError.:

AttributeError at /courses/2/create_answer/

The view:

@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", {'form': form, 'question': question})