从数据库传输到django dropbox

问题描述 投票:0回答:1

我正在尝试使用Django和HTML从我的数据库填充一个Dropbox。我一直试图弄清楚如何数小时,但它没有更新。

这是HTML代码:

<select id = "classChoice" >
    <option value = "base"> ----- </option>
    {% for class.name in objectlist %}
    <option value = "class.name"> {{ class.name }} </option>
    {% endfor %}
    </select>

这是forms.py:

class searchPageForm(forms.Form):
    className = forms.ModelChoiceField(queryset= Classroom.objects.all())
    studentName = forms.CharField(max_length=120)
    studentID = forms.CharField(max_length=20)

这是views.py:

def search(request):
    form = searchPageForm()
    if request.GET.get('class.name'):
        featured_filter = request.GET.get('class.name')
        objectlist = searchPageForm.objects.all()
        return render(request, 'pollingSite/search.html', {'objectlist': objectlist})
    else: 
        classNames = Classroom.objects.filter(instructor = request.user)
        return render(request, 'pollingSite/search.html', locals())

我被困住了,并尝试了所有的东西,它只是没有填充。

python django django-models django-forms django-views
1个回答
1
投票

在你的html页面中,{% for object in objectlist %}意味着它将迭代objectlist并将列表中的每个对象分配给object。这意味着您可以使用Classroom实例访问object的属性。所以改变html如下:

<select id="classChoice">
<option value = "base"> ----- </option>
    {% for object in objectlist %}      <!-- You were making mistake here -->
        <option value = "{{ object.id }}"> {{ object.name }} </option>
    {% endfor %}
</select>

在你的forms.py中:

class searchPageForm(forms.Form):
    className = forms.CharField(max_length=120)
    studentName = forms.CharField(max_length=120)
    studentID = forms.CharField(max_length=20)
© www.soinside.com 2019 - 2024. All rights reserved.