0

在我的第一个应用程序(课程)中,我正在创建课程。每门课程都有章节数,每章都有测验。我正在尝试使用第二个应用程序(测验)创建测验。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',),
    ]
4

2 回答 2

0

将您的 url 格式从更改/quiz/new/quiz/new/<chapter id>.

chapter id您可以从您的create quiz视图中获取这些。

于 2016-05-16T11:09:53.817 回答
0

您提到您在创建章节的页面中有一个用于创建测验的链接。我建议您在此处创建链接。

例如,

假设您有课程“Learn Python”,其中有一章名为 Introduction 并且它的 id 为 7,您可以像这样格式化您的 url /add-quiz/chapter/chapter_id。如果您从视图中传递它,您将在页面中有章节。

于 2016-05-16T11:12:32.597 回答