通过CreateView(使用modelform)创建对象时遇到问题。
代码优先:
查看:
class KlientCreateView(CreateView):
form_class = KlientCreateForm
model = Klient
template_name = 'forms/klient_pridat.html'
success_url = '/thanks'
URL配置:
url(r'^klient_pridat/$', KlientCreateView.as_view(), name='url_klient_pridat'),
模板:
{% block content %}
<form action="" method="post">{% csrf_token %}
<h3>formular</h3>
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
{% endblock %}
Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.7/site-packages/django/views/generic/base.py" in view
47. return self.dispatch(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
68. return handler(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/views/generic/edit.py" in post
172. return super(BaseCreateView, self).post(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/views/generic/edit.py" in post
138. return self.form_valid(form)
File "/usr/lib/python2.7/site-packages/django/views/generic/edit.py" in form_valid
113. return super(ModelFormMixin, self).form_valid(form)
File "/usr/lib/python2.7/site-packages/django/views/generic/edit.py" in form_valid
60. return HttpResponseRedirect(self.get_success_url())
File "/usr/lib/python2.7/site-packages/django/views/generic/edit.py" in get_success_url
101. url = self.success_url % self.object.__dict__
Exception Type: AttributeError at /klient_pridat/
Exception Value: 'bool' object has no attribute '__dict__'
填写表单并按提交后,我会看到错误页面
'bool' object has no attribute '__dict__'
从追溯和做一些搜索我猜它是由get_success_url()方法引起的(可能是由于形式错误的'动作'。我也试过了:
<form action="{% url url_klient_pridat %}" method="post">{% csrf_token %}
OR <form action="" method="post">{% csrf_token %}
OR <form action="/klient_pridat/" method="post">{% csrf_token %}
有什么想法吗?
答案 0 :(得分:0)
KlientCreateForm.save
应返回Klient
个实例。
现在,它正在返回bool
(即True
或False
)。