Django形式:在验证之前修改已发布数据的最佳方法是什么? [英] Django form: what is the best way to modify posted data before validating?
本文介绍了Django形式:在验证之前修改已发布数据的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
form = ContactForm(request.POST)
# how to change form fields' values here?
if form.is_valid():
message = form.cleaned_data['message']
在验证数据之前,有没有一种很好的方法来修剪空格,修改某些/所有字段等?
Is there a good way to trim whitespace, modify some/all fields etc before validating data?
推荐答案
您应该通过调用 copy
使request.POST( QueryDict
实例)可变然后更改值:
You should make request.POST(instance of QueryDict
) mutable by calling copy
on it and then change values:
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
QueryDict
文档-请求和响应对象
这篇关于Django形式:在验证之前修改已发布数据的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文