How to display “permission_denied_message” in custom 403.html page in Django

删除回忆录丶 提交于 2021-02-04 08:39:51

问题


How do we correctly display exception message in custom 403 error in Django?

In my signup view I am using:

class SignUpView(LoginRequiredMixin, PermissionRequiredMixin, CreateView):
    login_url = 'error_403'
    # ....
    # ...
    permission_denied_message = 'You are not allowed this transaction!! Ha Ha!!'
#    raise PermissionDenied() # ('permission_denied_message')
    raise_exception = True

Now how do I display the message ("permission_denied_message"?) in my custom 403.html?

This is my function to render the error page:

def error_403(request, exception):
    data = {}
    add_user = 'add_user'
    data = {'perm': add_user}
    return render(request, '403.html', data)

I have been through a lot of matter on the subject (including those on SO) but could not succeed in displaying the error message (except the static message that I have put in my "403.html"). I have used {{ exception}} in the template but nothing gets displayed (execpt the static message that I already have).

1. How do I display the permission_denied_message in custom 403 page?

2. Presently I am manually updating some raw data in dict data{} in function error_403, which gets rendered on the 403 error page. What is the correct usage of the dict?

来源:https://stackoverflow.com/questions/63167348/how-to-display-permission-denied-message-in-custom-403-html-page-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!