我尝试在使用' formtools'保存之前预览表单。当我访问帖子时,它会出现以下错误: 请求方法:GET 请求网址:http://127.0.0.1:8000/post/
Django Version: 1.10.1
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pagedown',
'bootstrapform',
'contact',
'crispy_forms',
'formtools',
'member']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _legacy_get_response
249. response = self._get_response(request)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ohid/test_venv/lib/python3.5/site-packages/formtools/preview.py" in __call__
34. return method(request)
File "/home/ohid/test_venv/lib/python3.5/site-packages/formtools/preview.py" in preview_get
58. context_instance=RequestContext(request))
Exception Type: TypeError at /post/
Exception Value: render_to_response() got an unexpected keyword argument 'context_instance'
这是我的preview.py:
from formtools.preview import FormPreview
from django.http import HttpResponseRedirect
from .models import Person
class PersonFormPreview(FormPreview):
form_template = 'member/person_form.html'
preview_template = 'member/person_review.html'
model = Person
def done(self, request, cleaned_data):
self.form.save()
# Do something with the cleaned_data, then redirect
# to a "success" page.
return HttpResponseRedirect('/form/success')
这是我的网址:
from .preview import PersonFormPreview
from .forms import MemberForm
from django import forms
url(r'^post/$', PersonFormPreview(MemberForm)),
如何修复此错误?
答案 0 :(得分:1)
formtools
不支持Django 1.10
版本,请{Djouo发布解决方法downgrade
。
答案 1 :(得分:1)
我通过lib / python3.5 / site-packages / formtools / preview.py文件中的一些更改解决了这个问题。这里我首先将render_to_response更改为render,然后从argument中删除context_instance = RequestContext(request)。假设post_post方法现在看起来像:
def post_post(self, request):
"""
Validates the POST data. If valid, calls done(). Else, redisplays form.
"""
form = self.form(request.POST, auto_id=self.get_auto_id())
if form.is_valid():
if not self._check_security_hash(
request.POST.get(self.unused_name('hash'), ''),
request, form):
return self.failed_hash(request) # Security hash failed.
return self.done(request, form.cleaned_data)
else:
return render(request, self.form_template,
self.get_context(request, form))
希望这会有所帮助。
答案 2 :(得分:0)
看起来它已经在他们的存储库中修复了:
https://github.com/django/django-formtools/blob/master/formtools/preview.py#L88
在此提交中:
https://github.com/django/django-formtools/commit/46b6839fb0063fa10ffb896ce80f7155ca7a9f6e
卸载django-formtools(BroadcastReceiver discoveryResult = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
BluetoothDevice remoteDevice;
remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (bluetooth.getBondedDevices().contains(remoteDevice)) {
foundDevices.add(remoteDevice);
aa.notifyDataSetChanged();
}
}
};
)并使用此pip命令安装库:
pip uninstall django-formtools