我在我的django项目中使用widget_tweaks
。对于输入标记,我使用它像:
{% render_field form.enrollment_no class='form-control' id='id_enrollment_number' placeholder='Enrollment Number' type='text' %}
现在,我希望为<select>
标记实现类似的功能:
<select class='form-control' id='id_faculty'>
{% for faculties in faculty %}
<option value="{{ faculties }}">{{ faculties }}</option>
{% endfor %}
</select>
但是,我认为我做错了,因为在检查表单提交的有效性时会有所帮助。请帮我解决这个问题。
编辑1: 此外,我从不同的模型获得教师:
form = StudentForm()
faculty = Faculty.objects.all()
return render(request, 'index.html',{'form' : form,'faculty' : faculty}).
斯图登模特:
class Student(models.Model):
"""here goes model for users"""
def __str__(self):
return self.name
name = models.CharField(max_length=200)
enrollment_no = models.CharField(max_length=10)
学院名称:
class Faculty(models.Model):
faculty_name = models.TextField()
def __str__(self):
return self.faculty_name
学生表格课程:
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = '__all__'
其他表:
class Faculty(models.Model):
faculty_name = models.TextField()
def __str__(self):
return self.faculty_name
class Department(models.Model):
faculty = models.ForeignKey(Faculty,on_delete=models.CASCADE)
department_name = models.TextField()
def __str__(self):
return self.department_name
class Course(models.Model):
student = models.ForeignKey(Student,on_delete=models.CASCADE)
department = models.ForeignKey(Department,on_delete=models.CASCADE)
course_name = models.TextField()
def __str__(self):
return self.course_name