为了账号安全,请及时绑定邮箱和手机立即绑定

请问怎么用foreignkey 实现Django下拉选择框?

/ 猿问

请问怎么用foreignkey 实现Django下拉选择框?

缥缈止盈 2019-07-08 14:10:57

怎么用foreignkey 实现Django下拉选择框


查看完整描述

5 回答

?
qq_遁去的一_1

首先,在django 视图函数中,传递 obj_list = [1, 2, 3] 类似这样的一个列表。def show_data(request):
obj_list = [1, 2, 3]
pass
return render_to_response('index.html', {'obj_list': obj_list})

然后在 index.html 模板文件中,部分代码如下
<select>
{% for obj in obj_list %}
<option value ="{{ forloop.counter }}">{{ obj }}</option>
{% endfor %}
</select>

这样,模板就能自动根据传递过来的数据,进行显示。
这样说的已经很详细了,如果还是不懂。自己去django官网看相应版本的文档去吧。

查看完整回答
反对 回复 2019-07-13
?
富国沪深

class ReportForm(forms.Form):    college = forms.ChoiceField(required=True, widget=forms.Select, choices=(),)     def set_choices(self):        colleges = College.objects.all()        self.fields['college'].choices = ()        for college in colleges:            self.fields['college'].choices += [(college.id, college.collegename)]

查看完整回答
反对 回复 2019-07-13
?
梵蒂冈之花

首先,在django 视图函数中,传递 obj_list = [1, 2, 3] 类似这样的一个列表。def show_data(request): obj_list = [1, 2, 3] pass return render_to_response('index.html', {'obj_list': obj_list}) 然后在 index.html 模板文件中

查看完整回答
反对 回复 2019-07-13
?
蓝山帝景

view plaincopy to clipboardprint?
from django import newforms as forms
from models import Language

# I put this on all required fields, because it's easier to pick up
# on them with CSS or JavaScript if they have a class of "required"
# in the HTML. Your mileage may vary.
attrs_dict = { 'class': 'required' }

class AddSnippetForm(forms.Form):
"""
Form used for adding Snippets.

"""
def __init__(self, *args, **kwargs):
super(AddSnippetForm, self).__init__(*args, **kwargs)
self.fields['language'].choices = [('', '----------')] + [(lang.id, lang.name) for lang in Language.objects.all()]

title = forms.CharField(max_length=250, widget=forms.TextInput(attrs=attrs_dict))
description = forms.CharField(widget=forms.Textarea(attrs=attrs_dict))
code = forms.CharField(widget=forms.Textarea(attrs=attrs_dict))
tag_list = forms.CharField(max_length=250, widget=forms.TextInput(attrs=attrs_dict))
language = forms.ChoiceField(choices=(), widget=forms.Select(attrs=attrs_dict))



查看完整回答
反对 回复 2019-07-13
?
慕后森

view plaincopy to clipboardprint?
from django import newforms as forms
from models import Language

# I put this on all required fields, because it's easier to pick up
# on them with CSS or JavaScript if they have a class of "required"
# in the HTML. Your mileage may vary.
attrs_dict = { 'class': 'required' }

class AddSnippetForm(forms.Form):
"""
Form used for adding Snippets.

"""
def __init__(self, *args, **kwargs):
super(AddSnippetForm, self).__init__(*args, **kwargs)
self.fields['language'].choices = [('', '----------')] + [(lang.id, lang.name) for lang in Language.objects.all()]

title = forms.CharField(max_length=250, widget=forms.TextInput(attrs=attrs_dict))
description = forms.CharField(widget=forms.Textarea(attrs=attrs_dict))
code = forms.CharField(widget=forms.Textarea(attrs=attrs_dict))
tag_list = forms.CharField(max_length=250, widget=forms.TextInput(attrs=attrs_dict))
language = forms.ChoiceField(choices=(), widget=forms.Select(attrs=attrs_dict))



查看完整回答
反对 回复 2019-07-13

添加回答

回复

举报

0/150
提交
取消
意见反馈 邀请有奖 帮助中心 APP下载
官方微信