AuxProc
AuxProc

Reputation: 331

How do you pass 'exception' argument to 403 view?

**Edit: Of course, it dawns on me that this doesn't have anything to do with the UserPassesTextMixin, because this error pops up when trying to visit the 403 page directly. Still not sure what to make of it though.

I'm attempting to use UserPassesTestMixin to check which model's edit view is being requested and run a test specific to that model to see if the user should have access. Nothing is working yet, I'm just trying to get a feel for how this mixin operates. Upon returning false in the test_func, the view tries to redirect to /403/, but I get the below error.

TypeError at /403/
permission_denied() missing 1 required positional argument: 'exception'

view

class DeviceUpdate(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Device
    template_name_suffix = '_update_form'
    form_class = DeviceUpdateForm

    def test_func(self):
        return edit_permission_test(self.get_object())

...

perms.py

def edit_permission_test(model_object):
    possible_models = ['Device',]
    if isinstance(model_object, Device):
        print('This is a Device model object')
        return True
    else:
        print('This doesnt look like a Device model object')
        return False

I cant seem to find anything out there on the interwebs that helps with this error.

Upvotes: 2

Views: 1046

Answers (1)

AuxProc
AuxProc

Reputation: 331

I think this issue just had to do with how the url patterns were configured for local development. Previously my main urls.py looked like this:

urlpatterns = [
    url(r'^$', TemplateView.as_view(template_name='pages/home.html'), name="home"),

    ...

    # Your stuff: custom urls includes go here
    url(r'^devices/', include('auto_toner.urls', namespace='auto_toner', app_name='auto_toner'), name="devices"),

    url(r'^400/$', default_views.bad_request),
    url(r'^403/$', default_views.permission_denied),
    url(r'^404/$', default_views.page_not_found),
    url(r'^500/$', default_views.server_error),


] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

I changed the URLs to include kwargs in the pattern if settings.DEBUG was True.

if settings.DEBUG:
    # This allows the error pages to be debugged during development, just visit
    # these url in browser to see how these error pages look like.
    urlpatterns += [
        url(r'^400/$', default_views.bad_request, kwargs={'exception': Exception('Bad Request!')}),
        url(r'^403/$', default_views.permission_denied, kwargs={'exception': Exception('Permission Denied')}),
        url(r'^404/$', default_views.page_not_found, kwargs={'exception': Exception('Page not Found')}),
        url(r'^500/$', default_views.server_error),
    ]
    if 'debug_toolbar' in settings.INSTALLED_APPS:
        import debug_toolbar

        urlpatterns += [
            url(r'^__debug__/', include(debug_toolbar.urls)),
        ]

Upvotes: 7

Related Questions