两个表格在一个模板DJANGO

时间:2018-04-02 11:29:58

标签: python django forms

是views.py

(
    [0] => Array
        (
            [0] => 1
            [1] => Mac1
            ....
            .... 
            [26] => Single
        )

    [1] => Array
        (
            [0] => 2
            [1] => Mac2
            ....
            .... 
            [26] => Single
        )

    [2] => Array
        (
            [0] => 3
            [1] => Mac3
            ....
            .... 
            [26] => Single
        )
   .............

是models.py

class StudentDetail(DetailView,FormView):
    model = Student
    template_name = 'student_detail.html'
    context_object_name = 'detail'
    form_class = AddComment

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

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            comment = Comment
            comment = form.save(commit=False)
            comment.created_by = request.user
            comment.save()
            student = get_object_or_404(Student,pk=kwargs['pk'])
            student.comments.add(comment)

            return redirect('/students/'+kwargs['pk'])

        else:
            print(form.errors)

        return FormView.post(self, request, *args, **kwargs) 

是form.py

class Comment(models.Model):
created_by = models.ForeignKey(User,related_name='comments',null=True,on_delete=models.CASCADE)
message = models.TextField(max_length=1000, unique=False)
about = models.CharField(max_length=1,choices=ABOUT_CHOICES,default='L')
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)

def __str__(self):
    return self.created_by.first_name +' : '+ self.message + ' : ' + str(self.comments.all())

class Score(models.Model):
    created_by = models.ForeignKey(User,related_name='scores',null=True,on_delete=models.CASCADE)
    subject = models.CharField(max_length=1,choices=SCORE_SUBJECT_CHOICES,default='A')
    korean = models.IntegerField(choices=SCORE_CHOICES,default='0')
    math = models.IntegerField(choices=SCORE_CHOICES,default='0')
    english = models.IntegerField(choices=SCORE_CHOICES,default='0')
    other1 = models.CharField(max_length=1,choices=OTHER_CHOICES,default='X')
    other2 = models.CharField(max_length=1,choices=OTHER_CHOICES,default='X')
    other1_score = models.IntegerField(choices=SCORE_CHOICES,default='0')
    other2_score = models.IntegerField(choices=SCORE_CHOICES,default='0')

    def __str__(self):
        return self.created_by.first_name +' : '+ self.get_subject_display() +' : ' + str(self.scores.all())


class Student(models.Model):
    school_year  = models.CharField(max_length=4,choices=SCHOOL_YEAR_CHOICES,default='2019')    
    campus = models.CharField(max_length=1,choices=CAMPUS_CHOICES,default='M')  
    type = models.CharField(max_length=1,choices=TYPE_CHOICES,default='N')  
    affiliate = models.CharField(max_length=1,choices=AFFILIATE_CHOICES,default='N')
    name = models.CharField(max_length=30, unique=True)
    entry_month = models.IntegerField(choices=MONTH_CHOICES,default=0)
    entry_day = models.IntegerField(choices=DAY_CHOICES,default=0)  
    comments = models.ManyToManyField(Comment,related_name='comments',blank=True)
    scores = models.ManyToManyField(Score,related_name='scores',blank=True) 
    picture = ProcessedImageField(
        upload_to = 'students/%Y/', 
        default = 'students/no-img.jpg',
        processors=[ResizeToFill(500,500)],
        options={'quality':80}      
        )
    def __str__(self):
        return self.name

ENV: python3 django 2.0

我正在使用'formview'和'detailview'在一个模板中使用'AddComment表单',如你所见,我想添加另一个表单来添加模型'得分'

我想要的一切,教师可以在学生详细信息模板中为每个学生添加评论,分数。

'详细视图'使教师可以使用分为单个学生的模板。作为url student / pk

是否有'formview'可以控制这两种类型的解决方案?

感谢。

0 个答案:

没有答案