我需要有关如何在Django

时间:2018-02-27 07:14:06

标签: python django django-forms django-templates

我正在尝试创建一种学生可以注册教师课程的方式。我在StudentData模型中添加了一个布尔字段,并从那里向我的Course模型添加了一个多对多字段。我的每个课程页面都是一个生成的slug页面,列出了所有学生。我希望每个学生附近都会显示一个复选框。如果教师选择更多学生并按下“注册”按钮,则只有学生才能查看该课程。

现在,模板条件我可以自己做,但我仍然坚持使用复选框以正确的方式更新数据。这是样板:

<ul>
    {% for student in students %}
        <br>
        <br>
        {{ student.name }} {{ student.surname }}<input type='checkbox' {% ifequal value 0 %}checked{% endifequal %}> 0
        <br>
        <br>
    {% endfor %}
</ul>
<div class="row">
    <div class="col-md-4">
        <div class="pagination">
            <span class="step-links">
                {% if students.has_previous %}
                    <a href="?page=1">&laquo; first</a>
                    <a href="?page={{ students.previous_page_number }}">previous</a>
                {% endif %}
                <span class="current">
                    {{ students.number }}
                </span>
                {% if students.has_next %}
                        <a href="?page={{ students.next_page_number }}">next</a>
                        <a href="?page={{ students.paginator.num_pages }}">last &raquo;</a>
                {% endif %}
            </span>
        </div>
    </div>
    <div class="col-md-4">
        <button class="btn-success">Enroll</button>    
    </div>
</div>


class StudentDataForm(forms.ModelForm):
    enrolled = forms.BooleanField()

    def __init__(self):
        if self.checked:
            self.fields['enrolled'].initial = True

    class Meta:
        model = StudentData
        fields = ('enrolled', )


class StudentData(models.Model):
    name = models.CharField(max_length=30)
    surname = models.CharField(max_length=50)
    student_ID = models.CharField(unique=True, max_length=14)
    notes = models.CharField(max_length=255, default=None, blank=True)
    enrolled = models.BooleanField(default=False)
    course = models.ManyToManyField('Course', default=None, blank=True)

class Course(models.Model):
    study_programme = models.ForeignKey('StudyProgramme', on_delete=models.CASCADE, default='')
    name = models.CharField(max_length=50, unique=True)
    ects = models.PositiveSmallIntegerField(validators=[MaxValueValidator(99)])
    description = models.TextField()
    year = models.PositiveSmallIntegerField(validators=[MaxValueValidator(99)])
    semester = models.IntegerField(choices=((1, "1"),
                                            (2, "2"),
                                            ), default=None)
    teacher1 = models.ForeignKey('TeacherData', on_delete=models.CASCADE, default=None,
                                 verbose_name="Course Teacher", related_name='%(class)s_course_teacher')
    teacher2 = models.ForeignKey('TeacherData', on_delete=models.CASCADE, default=None, null=True,
                                 verbose_name="Seminar Teacher", related_name='%(class)s_seminar_teacher')
    slug = models.SlugField(max_length=150, unique=True)

1 个答案:

答案 0 :(得分:1)

您不应在ModelForm中明确指定该字段,因为它已在模型中定义。

class StudentDataForm(forms.ModelForm):
    class Meta:
        model = StudentData
        fields = ('enrolled', )

这是表单定义。如果要传递初始值,则将其实例化为

form = StudentDataForm(initial={'enrolled': True})

它会神奇地(不是那样)创建最初enrolled字段选中的ModelForm实例。

现在在模板中,您只需渲染此表单(您必须在上下文context={'form': form}中的模板渲染中传递表单)

...
{{ form }}
...

这将呈现表单但如果您想手动呈现它,请按照这样做

...
{{ form.enrolled }}
...

它只会渲染复选框。请注意,您必须将其嵌套在<form>标记中,并在此处添加submit按钮。

更多资源:

Model Form | Django docs

Providing initial values | Django docs