Dex
Dex

Reputation: 365

Exception Value: context must be a dict rather than WSGIRequest

I'm making a web app using Django 1.11, Apache 2.2, and mod_wsgi all using Python 2.7. I'm having an issue rendering my page. The full traceback is,

Environment:


Request Method: GET
Request URL: http://website.com/~user/irtf/

Django Version: 1.11.1
Python Version: 2.7.13
Installed Applications:
['irtf.apps.IrtfConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'mod_wsgi.server']
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/path/irtf_website/mysiteenv/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/home/path/irtf_website/mysiteenv/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/home/path/irtf_website/mysiteenv/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/path/irtf_website/mysite/irtf/views.py" in index
  18.         return HttpResponse(template.render(request))

File "/home/path/irtf_website/mysiteenv/lib/python2.7/site-packages/django/template/backends/django.py" in render
  64.         context = make_context(context, request, autoescape=self.backend.engine.autoescape)

File "/home/path/irtf_website/mysiteenv/lib/python2.7/site-packages/django/template/context.py" in make_context
  287.         raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__)

Exception Type: TypeError at /irtf/
Exception Value: context must be a dict rather than WSGIRequest.

My views.py is,

def index(request):
        template = loader.get_template('irtf/index.html')

        return HttpResponse(template.render(request))

It might not matter but my template for this page just consists of text and links.

Upvotes: 3

Views: 9458

Answers (1)

e4c5
e4c5

Reputation: 53774

Which is because the first parameter is supposed to be a dictionary. You had perhaps mixed this up with the render short cut

template = loader.get_template('irtf/index.html')
return HttpResponse( template.render({}, request))

To use the render shortcut instead

return render(request,'irtf/index.html')

Upvotes: 15

Related Questions