使用引导选择框填充django表单 [英] Fill django form using bootstrap selectbox
问题描述
如何填写该选择框的项目?
html: / strong>
< div class =row>
< div class =col-md-6style =text-align:center;>
{%如果课程%}
< div class =form-group>
< label for =sel1class =yekan-smallstyle =text-align:center; color:#CC0033;> select lesson< / label>
< select multiple class =form-control yekan-smallid =lesson_select>
{%课程中的课程%}
< option> {{lesson.Code}} - {{lesson.Name}}< / option>
{%endfor%}
< / select>
< / div>
{%else%}
< div class =alert alert-danger yekan-small>错误< / div>
{%endif%}
< / div>
< div class =col-md-6style =text-align:center;>
{%如果学生%}
< div class =form-group>
< label for =sel1class =yekan-smallstyle =text-align:center; color:#CC0033;> select student< / label>
< select multiple class =form-control yekan-smallid =student_select>
{%student in student%}
< option> {{student.First_Name}} {{student.Last_Name}} - {{student.STNO}}< / option>
{%endfor%}
< / select>
< / div>
{%else%}
< div class =alert alert-danger yekan-small>错误< / div>
{%endif%}
< / div>
< div class =row>
< div class =col-md-6style =text-align:center;>
< label class =yekan-small> student< / label>< br />
{{form.student_code}}
< / div>
< div class =col-md-6style =text-align:center;>
< label class =yekan-small>课程< / label>< br />
{{form.lesson_code}}
< / div>
< / div>
< / div>
forms.py:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'''''''''''''''''''''''''''''''''''''' )
models.py:
class student(models.Model):
First_Name = models.CharField(max_length = 100,null = True)
Last_Name = models.CharField (max_length = 100,null = True)
STNO = models.CharField(max_length = 10,null = True)
类课(models.Model):
Name = models.CharField(max_length = 100,null = True)
代码= models.CharField(max_length = 100,null = True)
class lesson_student(models.Model):
lesson_code = models.CharField(max_length = 100 )
student_code = models.CharField(max_length = 100)
当我选择一个项目选择框,每个表单的字段填充该项目
,例如ni在学生的选择框中选择Student1 ...第一个学生表单的字段自动填充 {{student.STNO}}
是否可能?
您可以将其与一些JavaScript代码一起处理,以处理从下拉列表中复制学生和课程细节的选择
class lesson_student(models.Model):
lesson = models.ForeignKey(Lesson)
student = models.ForeignKey(Student)
现在,您不仅可以了解有关其代码的详细信息...您可以使用完整的模型。因为 ModelForm
将会照顾你,所以你也不会有任何其他事情要做。
< hr>
我的列表中有问题.....在学生或课程列表中我有学生对象& 课程对象而不是名称
Ie
$ b
class student(models.Model):
First_Name = models.CharField(max_length = 100,null = True)
Last_Name = models.CharField(max_length = 100,null = True)
STNO = models.CharField(max_length = 10,null = True)
def __str __(self) $ b return{0} {1}。format(self.First_Name,self.Last_Name)
i have a django form for add new data to my database & a Bootstrap Selectbox
how can i fill form with that selectbox's items ??
html :
<div class="row">
<div class="col-md-6" style="text-align:center;">
{% if lessons %}
<div class="form-group">
<label for="sel1" class="yekan-small" style="text-align:center; color:#CC0033;">select lesson</label>
<select multiple class="form-control yekan-small" id="lesson_select">
{% for lesson in lessons %}
<option>{{lesson.Code}} - {{lesson.Name}}</option>
{% endfor %}
</select>
</div>
{% else %}
<div class="alert alert-danger yekan-small">Error</div>
{% endif %}
</div>
<div class="col-md-6" style="text-align:center;">
{% if students %}
<div class="form-group">
<label for="sel1" class="yekan-small" style="text-align:center; color:#CC0033;">select student</label>
<select multiple class="form-control yekan-small" id="student_select">
{% for student in students %}
<option>{{student.First_Name}} {{student.Last_Name}} - {{student.STNO}}</option>
{% endfor %}
</select>
</div>
{% else %}
<div class="alert alert-danger yekan-small">Error</div>
{% endif %}
</div>
<div class="row">
<div class="col-md-6" style="text-align:center;">
<label class="yekan-small">student</label><br/>
{{form.student_code }}
</div>
<div class="col-md-6" style="text-align:center;">
<label class="yekan-small">lesson</label><br/>
{{form.lesson_code}}
</div>
</div>
</div>
forms.py :
class UnitSelectForm(forms.ModelForm):
class Meta:
model = lesson_student
fields = ('lesson_code','student_code')
models.py :
class student(models.Model):
First_Name = models.CharField(max_length=100,null=True)
Last_Name = models.CharField(max_length=100,null=True)
STNO = models.CharField(max_length=10,null=True)
class lesson(models.Model):
Name = models.CharField(max_length=100,null=True)
Code = models.CharField(max_length=100,null=True)
class lesson_student(models.Model):
lesson_code = models.CharField(max_length=100)
student_code = models.CharField(max_length=100)
when i select an item in Select Box , each form's field fill by that item
for example when i select Student1 in student's select box ... first student form's field automatically fill with {{student.STNO}}
is it possible ???
You could hack this together with some javascript code to handle the selection to copy the student and lesson details from the drop down boxes into the form but then why not just fix the model in the first place.
class lesson_student(models.Model):
lesson = models.ForeignKey(Lesson)
student = models.ForeignKey(Student)
Now, not only do you have details about their codes... you have the full models to available. You also will not have anything else left to do in terms of your form since the ModelForm
will take care of that.
i have problem in my list ..... in student or lesson list i have "Student Object" & "Lesson Object" instead of names
Thats because you haven't provided a string representation for your models.
I.e
class student(models.Model):
First_Name = models.CharField(max_length=100,null=True)
Last_Name = models.CharField(max_length=100,null=True)
STNO = models.CharField(max_length=10,null=True)
def __str__(self):
return "{0} {1}".format(self.First_Name, self.Last_Name)
这篇关于使用引导选择框填充django表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!