Django:在窗体初始化窗口小部件时获取当前url [英] Django : Get current url when init a widget in form
本文介绍了Django:在窗体初始化窗口小部件时获取当前url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据这个字符串,我想改变放在radioslect小部件中的数据。
我想根据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)
如果request.method =='POST':#如果表单已经提交...
function_form = FunctionForm(request .get_full_path,request.POST)
解决方案
你只需要在实例化表单时将请求对象作为kwarg传递。
I want to get the current url when I init my form to found within it a string I want to check. Depending on this string, I want to change the data I put in a radioselect widget. I would like to display different radioselect choices depending on the url.
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')
My view :
def add_function (request)
if request.method == 'POST': # If the form has been submitted...
function_form = FunctionForm(request.get_full_path,request.POST)
解决方案
You just have to pass in the request object as a kwarg when you instantiate the form.
这篇关于Django:在窗体初始化窗口小部件时获取当前url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文