Python 函数返回未在Django中呈现的多个复选框输入
我有一个数据库调用来检索myviews.py中的一些记录Python 函数返回未在Django中呈现的多个复选框输入,python,django,ajax,Python,Django,Ajax,我有一个数据库调用来检索myviews.py中的一些记录 def database(request): if request.method=="POST": #connect to the database #retrieve the results res = cur.fetchall() area_input = forms.DefineArea() tables_input = fo
def database(request):
if request.method=="POST":
#connect to the database
#retrieve the results
res = cur.fetchall()
area_input = forms.DefineArea()
tables_input = forms.DefineArea().print_tables(res)
cur.close()
con.close()
dictionary = {'area_input': area_input, 'tables_input': tables_input}
return render(request, "main/tables.html", context=dictionary)
在my forms.py中,我有:
from django import forms
class DefineArea(forms.Form):
area_name = forms.CharField(max_length=150)
@staticmethod
def print_tables(res):
tables = []
for row in res:
tables.append((row[0], row[0]))
tables_input = forms.MultipleChoiceField(choices=tables, required=False, widget=forms.CheckboxSelectMultiple())
return tables_input
我知道这是可行的,因为如果我返回表格而不是表格输入,我会看到所有表格都以HTML格式打印。但当我在Postman中测试它时,我看到的是对象对位置的引用:
但是如果我像我提到的那样改变它:
class DefineArea(forms.Form):
area_name = forms.CharField(max_length=150)
@staticmethod
def print_tables(res):
tables = []
for row in res:
tables.append((row[0], row[0]))
tables_input = forms.MultipleChoiceField(choices=tables, required=False, widget=forms.CheckboxSelectMultiple())
return tables
tables只是我从数据库中获得的表列表:
我需要做些什么来渲染Django对象吗