Fill django form using bootstrap selectbox [closed

2019-09-19 11:01发布

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 ???

1条回答
Melony?
2楼-- · 2019-09-19 11:26

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)
查看更多
登录 后发表回答