我想在我初始化表单时获取当前url,在其中找到我要检查的字符串。 根据此字符串,我想更改放在radioselect小部件中的数据。 我想根据网址显示不同的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)
答案 0 :(得分:3)
当您实例化表单时,您只需将请求对象作为kwarg传递。