0

当我初始化表单以在其中找到我要检查的字符串时,我想获取当前 url。根据这个字符串,我想更改我放入 radioselect 小部件的数据。我想根据 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)
4

1 回答 1

3

当您实例化表单时,您只需将请求对象作为 kwarg 传递。

于 2012-07-25T08:50:53.023 回答