我有一个BooleanField,在给定某些条件的情况下,需要将其禁用。
即使输入被禁用,如何提交输入
first time form submit
second time form submit会忽略禁用的布尔字段
views.py
{
"name": "LazyLoading",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"native-base": "^2.12.1",
"react": "^16.8.6",
"react-loadable": "^5.5.0",
"react-native": "^0.59.9",
"react-native-navigation": "^2.21.0",
"react-redux": "^7.0.3",
"redux": "^4.0.1",
"redux-form": "^8.2.3",
"redux-thunk": "^2.3.0"
},
"devDependencies": {
"@babel/core": "7.4.5",
"@babel/runtime": "7.4.5",
"babel-jest": "24.8.0",
"jest": "24.8.0",
"metro-react-native-babel-preset": "0.54.1",
"react-test-renderer": "^16.8.6"
},
"jest": {
"preset": "react-native"
}
}
每次禁用的BooleanField都返回false,即使已选择
i am also referred this link
forms.py
def test(request):
form = InformationForm()
test_table = TestTable.objects.get(id=1)
result = ''
if test_table.status == True:
result = True
form.fields['agree'].initial = True
form.fields['agree'].widget.attrs['disabled'] = True
# form.fields['agree'].widget.attrs={'onclick': 'return false','style':'opacity: 0.4 !important'}
if request.method == "POST":
post_form = InformationForm(request.POST)
result = post_form['agree'].value()
text = post_form['text'].value()
if post_form['agree'].value() == True:
t = TestTable.objects.filter(id=1).first()
t.status = 1
t.save()
post_form.fields['agree'].initial = True
post_form.fields['agree'].widget.attrs['disabled'] = True
#post_form.fields['agree'].widget.attrs={'onclick': 'return false','style':'opacity: 0.4 !important'}
return render(request,'test.html',context={'form':post_form,'result':result,'text':text})
text = form['text'].value()
return render(request,'test.html',context={'form':form,'result':result})
答案 0 :(得分:1)
这是Django的一项有意设计决定,因此用户无法编辑HTML输入并删除disabled
属性,这可以使用户更新其不打算更新的值。来自docs:
disabled
布尔参数设置为True
时,会使用disabled
HTML属性禁用表单字段,以使用户无法对其进行编辑。即使用户篡改了提交给服务器的字段的值,也将忽略该字段,而使用表单的初始数据中的值。
我建议更新表单类的__init__()
方法,以便仅在要禁用该字段时才添加disabled
属性。