I have this form:
我有这样的形式:
class CollaboratorForm(forms.Form):
user = forms.CharField(label="Username",max_length=100)
canvas = forms.IntegerField(widget=forms.HiddenInput)
....
def clean_user(self):
user = self.cleaned_data['user']
canvas = self.cleaned_data['canvas']
In the view I'm simply calling
在视图中我只是打电话
if form.is_valid():
I get the error:
我收到错误:
KeyError at /canvas/1/add-collaborator/
'canvas'
According to firebug the value is posting, it's just doesn't seem to be making it to my clean function. Am I doing it wrong?
根据萤火虫的价值是发布,它似乎似乎没有成为我的清洁功能。我做错了吗?
EDIT: Post data
编辑:发布数据
canvas 1
csrfmiddlewaretoken 2cb73be791b32ca9a41566082c804312
user username
EDIT2: I would also be willing to take an answer that could tell me how to send the primary key to the clean_user function, where the primary key is the /1/ in the example url above. The function in the view that is called is:
EDIT2:我也愿意接受一个可以告诉我如何将主键发送到clean_user函数的答案,其中主键是上面示例url中的/ 1 /。调用的视图中的函数是:
def canvas_add_collaborator(request, pk):
So I would want to send the pk to the clean_user function which would solve my problem by not needing the hidden field.
所以我想把pk发送到clean_user函数,它可以通过不需要隐藏字段来解决我的问题。
3 个解决方案
#1
9
You need to change the method name to clean(), not clean_user(). 'canvas' is not in the cleaned_data if you are just validating the user field.
您需要将方法名称更改为clean(),而不是clean_user()。如果您只是验证用户字段,'canvas'不在cleaning_data中。
#2
0
I solved my problem (probably not the best way, but works) using this:
我解决了我的问题(可能不是最好的方法,但有效)使用这个:
class CollaboratorForm(forms.Form):
....
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('canvas', None)
super(CollaboratorForm, self).__init__(*args, **kwargs)
Then in my view:
然后在我看来:
def canvas_add_collaborator(request, pk):
....
form.canvas = pk
Maybe not the most elegant solution, but it works for now. Feedback welcome.
也许不是最优雅的解决方案,但它现在可以使用。欢迎反馈。
#3
0
I found that the order in the declaration of fields matters, so if you want to access cleaned_data['canvas'] in the clean_user method, you must declare canvas first in your fields. I have tested this in Model forms
我发现字段声明中的顺序很重要,因此如果要在clean_user方法中访问cleaning_data ['canvas'],则必须先在字段中声明画布。我已经在模型表格中测试了这个
#1
9
You need to change the method name to clean(), not clean_user(). 'canvas' is not in the cleaned_data if you are just validating the user field.
您需要将方法名称更改为clean(),而不是clean_user()。如果您只是验证用户字段,'canvas'不在cleaning_data中。
#2
0
I solved my problem (probably not the best way, but works) using this:
我解决了我的问题(可能不是最好的方法,但有效)使用这个:
class CollaboratorForm(forms.Form):
....
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('canvas', None)
super(CollaboratorForm, self).__init__(*args, **kwargs)
Then in my view:
然后在我看来:
def canvas_add_collaborator(request, pk):
....
form.canvas = pk
Maybe not the most elegant solution, but it works for now. Feedback welcome.
也许不是最优雅的解决方案,但它现在可以使用。欢迎反馈。
#3
0
I found that the order in the declaration of fields matters, so if you want to access cleaned_data['canvas'] in the clean_user method, you must declare canvas first in your fields. I have tested this in Model forms
我发现字段声明中的顺序很重要,因此如果要在clean_user方法中访问cleaning_data ['canvas'],则必须先在字段中声明画布。我已经在模型表格中测试了这个