7

I'm having a bit of a problem, I uploaded my Django project to a webserver running apache, mod_python, and django. On the computer I developed on the following works fine

nameBox = getNamesBox().render(locals())

-

def getNamesBox():
    users = User.objects.filter()

    templateString = '<select name="name box">'
    for user in users:
        templateString += '<option value="' + user.name + '"> ' + user.name + '</option>'

    templateString += '</select>'

    template = Template(templateString)

    return template

But on the web server, when running from apache or manage.py runserver, it says

AttributeError at /order_site/order/
'dict' object has no attribute 'render_context'

The code on both machines is identical so I feel like maybe its some other issue? It can't render my form and I don't know why.

John Lotacs
  • 1,184
  • 4
  • 20
  • 34

1 Answers1

17

The render() method on a Template takes a Context object as its argument, not a dict. You'll have to construct a Context object from the dict, e.g.

namedbox = getNamesBox().render(Context(locals()))
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • 5
    Can be imported with `from django.template import Context` – azmeuk Aug 24 '16 at 19:11
  • 2
    in newer versions `render()` takes a dict as context which does not have this method – patroqueeet Aug 11 '17 at 09:20
  • 1
    @patroqueeet I have been said so too. But I have seen the `Template,render()` implementation of Django 1.11. There it tries to access render_context attribute of the passed dict object. In my case it raised an error when I passed a dict object. It says, "`dict object has no attribute 'render_context'` . " – sajid May 17 '18 at 09:09