当我初始化表单以在其中找到我想要检查的字符串时,我想获取当前的 url。
根据这个字符串,我想更改放入单选小部件中的数据。
我想根据 url 显示不同的 radioselect 选项。
class FunctionForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
# get url
url=request.get_full_path
# treat url
string = treat_url(url)
if (string=='ATC'):
self.fields['function_name'].queryset= FUNCTION_ATC_CHOICES
if (string=='ATS'):
self.fields['function_name'].queryset= FUNCTION_ATS_CHOICES
super(FunctionForm,self).__init__(*args,**kwargs)
#function_name = forms.ChoiceField(widget=RadioSelect,choices=FUNCTION_ATC_CHOICES)
class Meta :
model = Function
exclude =('session_number')
我的看法 :
def add_function (request)
if request.method == 'POST': # If the form has been submitted...
function_form = FunctionForm(request.get_full_path,request.POST)
当您实例化表单时,您只需将请求对象作为 kwarg 传递即可。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)