2016-05-16 126 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