2016-05-16 118 views
0

在我的第一个应用程序(课程)中,我创建了课程。每门课程都有多个章节,每章都有测验。我正在尝试使用第二个应用(测验)来创建测验。 models.py(测验):在Django中填充外键字段

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:/测验/新(使用来自测验应用程序的url.py),它由view.py(来自测验应用程序)表示。 问题是我不知道如何获取我创建测验的章节ID。章节链接的例子(用户点击创建测验之前的一个)/ 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',), 
    ] 
+0

分享您的网址conf – Anoop

回答

0

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

对于例如,

可以说,你当然有“学习Python”,这有一个叫言章,它有ID 7,您可以格式化你的URL作为这样/add-quiz/chapter/chapter_id。如果您从视图中传递它,您将在页面中拥有章节。

+0

This is current link: Create Quiz我怎么能通过ID从视图?我的url应该怎么样? – ssapp

+0

Create Quiz,你的urlconf可以是/ add-quiz/chapter /(?P \ d +)/。看到它给出你在这个特定的模板中有章节实例。 –

+0

gr8!还有一件事,是否可以将问题与测验配对,而不先创建测验(外键关系),还是必须针对问题制作另一个视图和模板?你明白我想问你什么吗? – ssapp

0

将您的网址格式从/quiz/new更改为/quiz/new/<chapter id>之类的内容。

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

+0

我如何获取章节ID是我没有得到..是否有可能以这种方式得到身份证,所以我可以创建测验并编辑它在相同的模板? – ssapp

+0

在创建章节API响应中,返回一个名为'chapter_id'的额外字段。在调用create quiz api时使用此chapter_id。多数民众赞成它 – Anoop

+0

你可以通过这个chapter_id像这样 Anoop