在Django中填充外键字段

时间:2016-05-16 10:12:21

标签: django foreign-keys template-inheritance

在我的第一个应用程序(课程)中,我正在创建课程。每门课程都有章节,每章都有测验。我试图使用第二个应用程序(测验)创建测验。 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(使用来自测验应用的url.py),由view.py(来自测验应用)表示。 问题我不知道如何获取我正在创建测验的章节的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',),

urls(测验):

urlpatterns = [
    url(r'^$', quiz.views.ListQuizView.as_view(),
        name='quiz-list',),
url(r'^new/$', quiz.views.CreateQuizView.as_view(),
    name='quiz-new',),
    ]

2 个答案:

答案 0 :(得分:0)

将您的网址格式从/quiz/new更改为/quiz/new/<chapter id>

您可以从chapter id视图中提取这些create quiz

答案 1 :(得分:0)

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

例如,

让我们说你有学习Python&#39;其中有一个名为“简介”的章节,它有id 7,您可以将您的网址格式化为/add-quiz/chapter/chapter_id。如果从视图中传递它,您将在页面中有章节。