猿问
下载APP

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

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


缥缈止盈
浏览 193回答 5
5回答

qq_遁去的一_1

首先,在django 视图函数中,传递 obj_list = [1, 2, 3] 类似这样的一个列表。def show_data(request):obj_list = [1, 2, 3]passreturn 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官网看相应版本的文档去吧。

富国沪深

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

梵蒂冈之花

首先,在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 模板文件中

蓝山帝景

view plaincopy to clipboardprint?from django import newforms as formsfrom 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&nbsp;__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))

慕后森

view plaincopy to clipboardprint?from django import newforms as formsfrom 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&nbsp;__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))
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答