Surprising behaviour in case of exception in form widget when DEBUG=False

67 views
Skip to first unread message

Federico Capoano

unread,
Mar 11, 2016, 6:30:35 AM3/11/16
to Django developers (Contributions to Django itself)
This never happened to me and I find it very strange. Maybe it's a bug or maybe it's the intended behaviour, but I find it REALLY strange.

Consider a widget which, for some reason, raises an exception:

class CoolWidget(AdminTextareaWidget):
    def render(self, name, value, attrs={}):
        WRONG()
        return super(JsonSchemaWidget, self).render(name, value, attrs)

The widget is included in an admin form like:

class PizzaForm(forms.ModelForm):
    class Meta:
        model = Pizza
        exclude = []
        widgets = {'toppings': CoolWidget}


class PizzaAdmin(ModelAdmin):
    form = TemplateForm

Now, if DEBUG is True, trying to open the relative pizza change or add form will result in:

NameError: name 'WRONG' is not defined

When DEBUG to False what I see is really strange:

I get an empty change/add form!


Now suppose that the condition that raises the exception happens only on a remote server

which has DEBUG=False. The exception happens because the configuration of a django project

has not been updated correctly yet.


Instead of failing loud, it will fail silently, leaving an empty add/change form.


Is it the intended behaviour? Or a bug?

I guess it is related due to the fact that template errors are silenced.


Federico

Tim Graham

unread,
Mar 11, 2016, 7:18:27 AM3/11/16
to Django developers (Contributions to Django itself)
Yes, I think it's {% include %} rendering an empty string if there's an exception. In Django 1.9, we added a logger for these exceptions to make debugging easier: https://docs.djangoproject.com/en/stable/topics/logging/#django-template.

Federico Capoano

unread,
Mar 11, 2016, 9:35:54 AM3/11/16
to Django developers (Contributions to Django itself)
Right, I see the log entries. Good to know there's a way to log them, for those who use sentry it will be even easier to notice this problem,
just ensure you're logging WARNING messages.

Thanks
Federico
Reply all
Reply to author
Forward
0 new messages