dj pro dj pro - 1 month ago 19
Python Question

TypeError at /post/ render_to_response() got an unexpected keyword argument 'context_instance'

I am trying to preview form before saving using 'formtools'. When I visit post it gives following errors:
Request Method: GET
Request URL: 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'


Here is my 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')


Here is my urls:

from .preview import PersonFormPreview
from .forms import MemberForm
from django import forms

url(r'^post/$', PersonFormPreview(MemberForm)),


How do I fix this errors?

Answer

I solved it by some changes in lib/python3.5/site-packages/formtools/preview.py file. Here I first changes render_to_response to render and then removed context_instance=RequestContext(request) from argument. Suppose post_post method now look like:

 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))

Hope this will help others.

Comments