49

I've upgraded to Django 2.1, and I'm seeing this error when I load the admin interface:

TypeError at /admin/foo/bar/1/change/

render() got an unexpected keyword argument 'renderer'
Flimm
  • 136,138
  • 45
  • 251
  • 267

4 Answers4

114

This is almost certainly because of this backwards-incompatible change in Django 2.1:

  • Support for Widget.render() methods without the renderer argument is removed.

You may have subclassed django.forms.widgets.Widget in your code, or in the code of one of your dependencies. The code may look like this:

from django.forms import widgets

class ExampleWidget(widgets.Widget):
    def render(self, name, value, attrs=None):
        # ...

You need to fix the method signature of render, so that it looks like this:

    def render(self, name, value, attrs=None, renderer=None):

Have a look at the source code of widgets.Widget if you want to check.

Flimm
  • 136,138
  • 45
  • 251
  • 267
  • 3
    The current version of `django-datetime-widget` results in this when used with Django 2.1, although there is a pull request to fix it currently pending. – Gwyn Evans Sep 21 '18 at 20:45
  • 2
    Except the traceback only lists core Django files, and no custom code, so it's unclear what widget is causing the error. – Cerin Feb 02 '19 at 03:36
  • For me, was due to using https://pypi.org/project/django-jsonfield/ with django 2.1, related to the widget I guess – Vadorequest Feb 27 '19 at 23:42
  • 2
    Exactly right, frustrating that you can't get a decent stack trace on this error, as the root cause could be in any number of dependencies, or in your own code. Thankfully, an easy fix. I think the best way of finding this error is to just attempt a Ctrl+Shift+F on your project and look for the outdated method signature – M. Ryan Sep 27 '19 at 15:58
  • @GwynEvans The project on PyPI [`django-datetime-widget`](https://pypi.org/project/django-datetime-widget/) hasn't been updated since 2014. The fork on PyPI [`django-datetime-widget2`](https://pypi.org/project/django-datetime-widget2/) seems to have a fix for Django >= 2.1 – Flimm Feb 09 '22 at 11:01
  • @Vadorequest The project on PyPI https://pypi.org/project/django-jsonfield/ should support Django >= 2.1 now. – Flimm Feb 09 '22 at 11:04
0

Django is looking for a default renderer which can be set in settings.py

FORM_RENDERER = 'django.forms.renderers.DjangoTemplates'
  • 1
    Welcome to Stack Overflow! You're right. I think this by itself does not solve the error in the question. This sort of information is best added as a comment, but I realise that you don't have enough points yet to post comments, unfortunately. Bon courage! – Flimm May 19 '20 at 09:51
0

The actual issue with this problem is in as_widget() function of BoundField class located at the location:

your_env_path/lib/python3.11/site-packages/django/forms/boundfield.py

existing_code

def as_widget(self, widget=None, attrs=None, only_initial=False):
    #other code
    response = widget.render(
        name=self.html_initial_name if only_initial else self.html_name,
        value=value,
        attrs=attrs,
        renderer=self.form.renderer,
        )
    return response
 
#**update the above code to**
def as_widget(self, widget=None, attrs=None, only_initial=False):
    #other code
    try:
        response = widget.render(
        name=self.html_initial_name if only_initial else self.html_name,
        value=value,
        attrs=attrs,
        renderer=self.form.renderer,
        )
    except:
        response = widget.render(
        name=self.html_initial_name if only_initial else self.html_name,
        value=value,
        attrs=attrs
        )
    return response`
-3

Its version and signature incompatibility issue. Go back to version - 2.0.8

pip3 install Django==2.0.8

rk_cha_py
  • 29
  • 3
  • The old, accepted answer explains the incompatibility problem in much more detail, and also provides a reasonable solution. Downgrading Django to solve a minor issue doesn't constitute one. – Thierry Lathuille Feb 27 '21 at 18:04