2017-08-15 74 views
0

我使用ClassroomCreateView来选择一个教室。然后我想显示使用ClassroomDetailView选择的教室。这将显示该教室中的学生名单。我想我应该可以使用pk并从ModelForm到DetailView进行遍历。如何将pk从ModelForm传递给DetailView?

课堂教学模式

class Classroom(models.Model): 
    BLOCK_NUMBER = (
     ('11', 'Block 1-1'), 
     ('12', 'Block 1-2'), 
     ('13', 'Block 1-3'), 
     ('14', 'Block 1-4'), 
     ('21', 'Block 2-1'), 
     ('22', 'Block 2-2'), 
     ('23', 'Block 2-3'), 
     ('24', 'Block 2-4'), 
    ) 
    class_list = models.TextField() 
    course_block = models.CharField(max_length=10, choices=BLOCK_NUMBER) 

    def __str__(self): 
     return self.get_course_block_display() 

    def save(self, *args, **kwargs): 
     super(Classroom, self).save(*args, **kwargs) 
     # overrides the default save function to parse the class list 
     studentList = [] 
     studentList = self.class_list.split('\n') 
     print (studentList) 
     for line in studentList: 
      line = line.strip('\r') 
      s = Student.objects.create(nickname = line, classroom = self) 

学生模型

class Student(models.Model): 
    classroom = models.ForeignKey(Classroom, on_delete=models.CASCADE) 
    nickname = models.CharField(default='JohnS', max_length=31) 

Forms.py

class AttendForm(ModelForm): 
    class Meta: 
     model = Classroom 
     fields = ['course_block'] 

    def __init__(self, *args, **kwargs): 
     super(AttendForm, self).__init__(*args, **kwargs) 

Views.py

class AttendCreateView(CreateView): 
    model = Classroom 
    form_class = AttendForm 
    template_name = 'classroom/attend_form.html' 

    def get_success_url(self): 
     return reverse('classroom:random', self.kwargs['pk']) 

create_attendance_view = AttendCreateView.as_view() 

这是我想念的部分。 course_block有一个PK,但我没有从形式传递到视图。

class ClassroomDetailView(DetailView): 
    model = Classroom 
    template_name = 'classroom/random_list.html' 

    def get_context_data(self, **kwargs): 
     class_pk = self.kwargs['pk'] 
     context = super(ClassroomDetailView, self).get_context_data(**kwargs) 
     students = Student.objects.filter(pk = 'class_pk') 
     context['students'] = students 
     return context 

detail_classroom_view = ClassroomDetailView.as_view() 

urls.py

url(r'^classup/$', create_classroom_view, name='classroom'), 
url(r'^attend/$', create_attendance_view, name='students'), 
url(r'^(?P<pk>[0-9]+)/$', detail_classroom_view, name='random'), 

如何从我的形式选择详细视图去(可能使用选择的PK)?

+0

显示你的'urls.py',即'教室:随机'是什么。另外,发生了什么?你会得到一个错误,模板中有错误的链接,还有其他的东西吗? –

+0

上面编辑显示urls.py。当我使用self.object.id时,在AttendCreateView上提交AttendForm后,ClassroomDetailView中的id#会自动增加。即.../45 /然后/ 46 /等等。我在ClassroomDetailView中打印了一个print语句来打印self.kwargs ['pk'],它会打印出“none”。没有任何错误。 –

+0

好吧,只有现在我明白你想要做什么。它实际上非常类似于[今日堆栈溢出的前一个问题](https://stackoverflow.com/questions/45699023/displaying-other-attribute-values-if-one-is-known-in-django-template/45699325 ),但是她正在使用基于函数的视图('index'),您正试图使用​​'CreateView'。我同意你对评论的回答,如果我使用基于类的视图,那么我将使用[FormView](https://docs.djangoproject.com/en/1.11/ref/class-based-意见/通用编辑/#FormView控件)。 –

回答

0

你需要的对象ID,尝试

def get_success_url(self): 
    return reverse('classroom:random', self.object.id) 

通过文档

object¶

当使用CreateView的你有机会获得self.object,这是正在创建的对象。如果该对象尚未创建,则该值为无。

+0

我曾试过。它似乎只是自动递增,并不对应于所选对象的pk。它没有给予pk。也许我在使用CreateView时是错误的,因为我并不是要真正创建一个对象。我只想使用AttendForm选择一个。 –

相关问题