Django raising 404 with a message

后端 未结 9 1169
有刺的猬
有刺的猬 2020-12-19 05:31

I like to raise 404 with some error message at different places in the script eg: Http404(\"some error msg: %s\" %msg) So, in my urls.py I included:

<         


        
9条回答
  •  一向
    一向 (楼主)
    2020-12-19 06:30

    You can return a plain HttpResponse object with a status code (in this case 404)

    from django.shortcuts import render_to_response
    
    def my_view(request):
        template_context = {}
    
        # ... some code that leads to a custom 404
    
        return render_to_response("my_template.html", template_context, status=404)
    

提交回复
热议问题