在我的第一个应用程序(课程)中,我正在创建课程。每门课程都有章节数,每章都有测验。我正在尝试使用第二个应用程序(测验)创建测验。models.py(测验):
class Quiz(models.Model):
coursechapter = models.ForeignKey(CourseChapter)
name = models.CharField(max_length=255, verbose_name=u'Quiz name',)
creator = models.ForeignKey(User)
creation = models.DateField(auto_now_add=True)
def __unicode__ (self):
return self.name
class Question(models.Model):
quiz = models.ForeignKey(Quiz)
text = models.CharField(max_length=255, verbose_name=u'Question\'s text')
class QuestionAnswer(models.Model):
question = models.ForeignKey(Question)
text = models.CharField(max_length=255, verbose_name=u'Answer\'s text')
is_valid = models.BooleanField(default=False)
class UserAnswer(models.Model):
answer = models.ForeignKey(QuestionAnswer)
我有用于创建课程的模板,在该模板中我有链接(添加章节),它将我带到另一个模板(视图)来创建章节。在里面我有链接来为那个特定的章节创建测验。该链接指向 url: /quiz/new (using url.py from quiz app),它由 view.py (from quiz app) 表示。 问题是我不知道如何获取我正在为其创建测验的章节的 ID。章节 URL 示例(用户单击创建测验之前的一个) /course/new/chapter/197/ ,是否可以通过链接以某种方式发送 chapter_id (197) 还是有其他方式?views.py(测验):
class CreateQuizView(CreateChapterView):
model = Quiz
template_name = 'quiz/create_quiz.html'
fields = '__all__'
def dispatch(self, request, *args, **kwargs):
self.pk = kwargs.get('pk')
return super(CreateQuizView, self).dispatch(request, *args, **kwargs)
def get_success_url(self):
return reverse('quiz-list',
kwargs={'pk': Quiz.objects.latest('id').id})
def get_context_data(self, **kwargs):
context = super(CreateQuizView, self).get_context_data(**kwargs)
return context
views.py(课程):
class CreateChapterView(CreateView, GroupRequiredMixin):
model = CourseChapter
template_name = 'course/add_chapter.html'
fields = '__all__'
def dispatch(self, request, *args, **kwargs):
self.pk = kwargs.get('pk')
return super(CreateChapterView, self).dispatch(request, *args, **kwargs)
def get_success_url(self):
return reverse('courses-chapters',
kwargs={'pk': Course.objects.latest('id').id})
def get_context_data(self, **kwargs):
context = super(CreateChapterView, self).get_context_data(**kwargs)
context['chapter'] = CourseChapterForm
context['chapters'] = CourseChapter.objects.all()
context['last'] = Course.objects.latest('id')
context['courses'] = Course.objects.all()
context['action'] = reverse('courses-chapters',
kwargs={'pk': Course.objects.latest('id').id})
context['kw'] = self.kwargs
context['quiz'] = QuizForm()
context['question'] = QuestionForm()
context['answer'] = QuestionAnswerForm
return context
def form_valid(self, form):
self.object = form.save()
# chapters = CourseChapter.objects.filter(course_id=Course.id)
return HttpResponseRedirect(self.get_success_url())
urls.py(主要)
url(r'^course/', include('course.urls')),
url(r'^quiz/', include('quiz.urls', namespace="quiz")),
网址(课程)
url(r'^new/$', course.views.CreateCourseView.as_view(),
name='courses-new',),
url(r'^new/(?P<pk>\d+)/$', course.views.CreateChapterView.as_view(),
name='courses-chapters'),
url(r'^edit/(?P<pk>\d+)/$', course.views.UpdateCourseView.as_view(),
name='courses-edit',),
url(r'^new/chapter/(?P<pk>\d+)/$', course.views.CreateChapterView.as_view(),
name='chapter-content',),
url(r'^edit/chapters/(?P<pk>\d+)/$', course.views.UpdateChapterView.as_view(),
name='chapters-edit',),
网址(测验):
urlpatterns = [
url(r'^$', quiz.views.ListQuizView.as_view(),
name='quiz-list',),
url(r'^new/$', quiz.views.CreateQuizView.as_view(),
name='quiz-new',),
]