**编辑:当然,我发现这与UserPassesTextMixin没有任何关系,因为在尝试直接访问403页面时会弹出此错误。但仍然不确定该怎么做。
我正在尝试使用UserPassesTestMixin来检查正在请求哪个模型的编辑视图,并运行特定于该模型的测试以查看用户是否应该具有访问权限。什么都没有用,我只是想了解一下这个mixin是如何运作的。在test_func中返回false后,视图尝试重定向到/ 403 /,但我得到以下错误。
TypeError at /403/
permission_denied() missing 1 required positional argument: 'exception'
视图
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
我似乎无法在互联网上发现有助于解决此错误的任何内容。
答案 0 :(得分:2)
我认为这个问题只与如何为本地开发配置url模式有关。以前我的主要urls.py看起来像这样:
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更改为在模式中包含kwargs。
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)),
]