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 trialDongyun Cho
2,256 Pointscannot import name forms
courses/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_vaild():
quiz = form.save(commit=False)
quiz.course = course
quiz.save()
messages.add_message(request, messages.SUCCESS, "Quiz Added")
return HttpResponseRedirect(quiz.get_absulte_url())#reverseμν¨.
return render(request, 'courses/quiz_form.html',{'form':form, 'course':course})
forms.py
from django import forms
from . import models
class QuizForm(forms.ModelForm):
class Meta:
model = models.Quiz
fields = [
'title',
'description',
'order',
'total-questions',
]
# exclude = ['course', ]
class TrueFalseQuestionForm(forms.ModelForm):
class Meta:
model = models.TrueFalseQuesion
fields = ['order', 'prompt']
class MultipleChoiceQuestionForm(forms.ModelForm):
class Meta:
model models.MultipleChoiceQuestion
fields = ['order', 'prompt', 'shuffle_answers']
I dont know what is wrong here! when I sun server, it makes an error, saying ImportError : cannot import name 'forms'.
It seems that my code is just the same with teacher's code.
1 Answer
Basel Barzanji
Python Web Development Techdegree Graduate 20,706 PointsThis might be unrelated you also have a typo in your TrueFalseQuestionForm:
class TrueFalseQuestionForm(forms.ModelForm):
class Meta:
model = models.TrueFalseQuesion #<--Missing a 't' here in ...Question
fields = ['order', 'prompt']
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsWhich import forms is causing the error? Can you post the full stacktrace output?