django tutorial: custom 404 and 500 views

后端 未结 1 1420
無奈伤痛
無奈伤痛 2021-01-18 13:06

Windows 7
Python 2.7.3
Django 1.5
python manage.py runserver

I am following the tutorial as available at \'https://docs.djangoproject.com/en/1.5/intro/tu

相关标签:
1条回答
  • 2021-01-18 13:35

    As I can see you have a little mistake, but dont worry.

    Look, try this:

    handler404 = 'mysite.views.error404'
    

    Then in mysite (the second mysite) creates a views.py if you dont have. In that views.py put:

    from django.shortcuts import render
    
    def error404(request):
        return render(request,'404.html')
    

    and thats all! In templates, create that file and do something nice!

    Remember that only works in production (DEBUG=False) otherwise django use the traceback. Is the same for 505, and you maybe can try handler404 = 'polls.views.see404' and put in the views.py then but you know, tastes are different hehehe.

    Now, try something else, for example: comment in the urls.py

    #handler404 = 'mysite.views.error404'

    and remove too in the views.py the def.

    In the index of your templates file creates a simple 404.html. Run the server in production and produce that 404 error, what happened?

    0 讨论(0)
提交回复
热议问题