django python开发中表单其中一项设置动态下拉菜单,如何让下拉...
2016-05-16
展开全部
django python开发中表单其中一项设置动态下拉菜单,其中下拉菜单的选项是从接口获取的对象集合:
代码如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, sys
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponse,HttpResponseRedirect
import requests
from django import forms
from blog.models import Info
attrs_dict={'class':'required'}
class InfoForm(forms.Form):
def _init_(self,*args,**kwargs):
r=requests.get('https://223.252.197.115/api/getallpdts...',**kwargs)
print r
super(InfoForm,self)._init_(*args,**kwargs)
self.fields['product'].choices=[('','-------------')]+[(info.id,info.name) for info in r]
product = forms.ChoiceField(label="产品名称",choices=(),widget=forms.Select(attrs=attrs_dict))
代码如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, sys
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponse,HttpResponseRedirect
import requests
from django import forms
from blog.models import Info
attrs_dict={'class':'required'}
class InfoForm(forms.Form):
def _init_(self,*args,**kwargs):
r=requests.get('https://223.252.197.115/api/getallpdts...',**kwargs)
print r
super(InfoForm,self)._init_(*args,**kwargs)
self.fields['product'].choices=[('','-------------')]+[(info.id,info.name) for info in r]
product = forms.ChoiceField(label="产品名称",choices=(),widget=forms.Select(attrs=attrs_dict))
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询