This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable,
visit the help center。
已关闭8年。
我正在尝试编写非常简单的代码-通用FormView遇到了一个大问题。
#views.py
class FooView(FormView):
template_name = 'foo.html'
form_class = FooForm
def get_success_url(self):
return reverse('foo')
def get_context_data(self, **kwargs):
#...
return context
def form_valid(self, form):
form.save()
return super(FooView, self).form_valid(form)
def get_form_kwargs(self):
return {'foo':'bar'}
#forms.py
class FooForm(forms.ModelForm):
def __init__(self, foo, *args, **kwargs):
#...
super (FooForm, self).__init__(*args, **kwargs)
def clean_abc(self):
#...
def clean_contact(self):
#...
class Meta:
model = Foo
#foo.html
...
<div id="form_add_foo">
<form method="post" class="standard" id="form_add_foo">
{% csrf_token %}
{{ form.as_p }}
<p><input type="submit" name="form_add_foo" value="Add Foo"></p>
</form>
</div>
#request.raw_post_data == csrfmiddlewaretoken=99a43151f0198b7c47541c1e91b230e5&a=2&b=1&c=1&form_add_foo=Add+Foo
#form.data == {u'b': [u'1'],
u'csrfmiddlewaretoken': [u'99a43151f0198b7c47541c1e91b230e5'],
u'form_add_foo': [u'Add Foo'],
u'c': [u'1'],
u'a': [u'2']}
我的问题是-为什么form.data值是列表,而不仅仅是raw_post_data中的unicode值?第二个问题-我该如何改变这种行为?