Mat*_*our -2 forms django validation post
我有一个django表单,我需要为验证目的设置一个值,该值不作为标准Post请求的一部分传递.
在我的代码中,我目前有类似的东西:
if request.method == 'POST':
postform = CreatePostForm(request.POST, request.FILES, initial={'post_type':post.post_type})
if postform.is_valid():
.....
Run Code Online (Sandbox Code Playgroud)
值post_type是一个值为'QUE'的选择
我遇到的问题是这似乎没有效果.有没有人有任何关于如何在验证发生之前将post_type值添加到CreatePostForm类的建议.
请注意我不想在表单上公开这个值,因此包含它作为帖子的一部分不是一个选项.
一种方法是将它作为表单类的属性,在实例化时将其作为参数传递:
class CreatePostForm(forms.Form/ModelForm):
def __init__(self, post_type, *args, **kwargs):
super(CreatePostForm, self).__init__(*args, **kwargs)
self.post_type = post_type
postform = CreatePostForm(post_type=post.post_type, request.POST, request.FILES)
Run Code Online (Sandbox Code Playgroud)
希望能帮到你.
| 归档时间: |
|
| 查看次数: |
4582 次 |
| 最近记录: |