如何在Django中针对错误400错误请求测试我的自定义错误页面
views.py
class Custom400View(TemplateView):
template_name = "400.html"
urls.py
handler400 = Custom400View.as_view()
答案 0 :(得分:1)
在veiws.py
def handler400(request):
return render(request, '400.html', status=400)
在urls.py
中handler400 = myapp.views.handler400
注意使调试保持错误
有关更多详细信息,请参见this
答案 1 :(得分:0)
您可以设置一个仅引发这样的SuspiciousOperation异常的视图
from django.core.exceptions import SuspiciousOperation
def bad_request_view(request):
raise SuspiciousOperation
然后在您的网址py中添加视图的网址
链接到文档说明:https://docs.djangoproject.com/en/2.1/topics/http/views/#testing-custom-error-views
答案 2 :(得分:0)
请使用SuspiciousOperation引发错误请求400错误
from django.core.exceptions import SuspiciousOperation
def custom():
raise SuspiciousOperation('bad request')