如何将“ exception”参数传递给403视图? [英] How do you pass 'exception' argument to 403 view?

查看:96
本文介绍了如何将“ exception”参数传递给403视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**编辑:当然,我突然意识到这与UserPassesTextMixin没有任何关系,因为在尝试直接访问403页面时会弹出此错误。仍然不确定该怎么做。

** 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.

我正在尝试使用UserPassesTestMixin来检查请求哪个模型的编辑视图,并运行特定于该模型的测试以查看用户是否应具有访问权限。目前还没有任何工作,我只是想了解一下这种mixin的工作方式。在test_func中返回false时,视图尝试重定向到/ 403 /,但出现以下错误。

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

推荐答案

我认为这个问题与如何针对本地开发配置网址格式。以前我的主要urls.py看起来像这样:

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)

如果settings.DEBUG为True,我将URL更改为在模式中包含kwarg。

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)),
        ]

这篇关于如何将“ exception”参数传递给403视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆