form = ContactForm(request.POST)
# how to change form fields' values here?
if form.is_valid():
message = form.cleaned_data['message']
Run Code Online (Sandbox Code Playgroud)
在验证数据之前,有没有一种很好的方法来修剪空白,修改一些/所有字段等?
ndp*_*dpu 14
您应该QueryDict
通过调用copy
它来使request.POST(instance of )可变,然后更改值:
post = request.POST.copy() # to make it mutable
post['field'] = value
# or set several values from dict
post.update({'postvar': 'some_value', 'var': 'value'})
# or set list
post.setlist('list_var', ['some_value', 'other_value']))
# and update original POST in the end
request.POST = post
Run Code Online (Sandbox Code Playgroud)
QueryDict
docs - 请求和响应对象
小智 7
您也可以尝试使用request.query_params
.
首先,_mutable
将 的属性设置query_params
为True
。
更改您想要的所有参数。
request.query_params._mutable = True
request.query_params['foo'] = 'foo'
Run Code Online (Sandbox Code Playgroud)
这里的优点是您可以避免使用request.POST.copy()
.
归档时间: |
|
查看次数: |
3285 次 |
最近记录: |