6

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/tutorial03/'

with DEBUG=True in settings.py the webpages are generated correctly. The address

http://127.0.0.1:8000/admin/  

displays the admin login page.

http://127.0.0.1:8000/polls/  

displays the 'What's up?' bulleted link.

http://127.0.0.1:8000/polls/1/  

displays the number '1'

http://127.0.0.1:8000/polls/2/  

displays the standard 404 message. However if I set DEBUG=False (and do nothing else) then I get a 500 error at ALL ADDRESSES! I am using the development internal runserver. There are no traceback errors. The server log looks like this:

with DEBUG=True

0 errors found  
March 19, 2013 - 12:01:12  
Django version 1.5, using settings 'mysite.settings'  
Development server is running at http://127.0.0.1:8000/  
Quit the server with CTRL-BREAK.  
[19/Mar/2013 12:06:41] "GET /admin/ HTTP/1.1" 200 1893  
[19/Mar/2013 12:54:55] "GET /polls/ HTTP/1.1" 200 74  
[19/Mar/2013 12:55:02] "GET /polls/2/ HTTP/1.1" 404 1577  
[19/Mar/2013 12:55:09] "GET /polls/1/ HTTP/1.1" 200 1  
Validating models...  

with DEBUG=False

0 errors found  
March 19, 2013 - 12:55:21  
Django version 1.5, using settings 'mysite.settings'  
Development server is running at http://127.0.0.1:8000/  
Quit the server with CTRL-BREAK. 
[19/Mar/2013 12:59:22] "GET /admin/ HTTP/1.1" 500 27  
[19/Mar/2013 12:59:25] "GET /polls/ HTTP/1.1" 500 27  
[19/Mar/2013 12:59:28] "GET /polls/1/ HTTP/1.1" 500 27  
[19/Mar/2013 12:59:34] "GET /polls/2/ HTTP/1.1" 500 27  

My directory structure is as follows:

1> mysite  
2>     - mysite  
3>     - polls  
4>         - templates  
5>             - polls  
6>     - templates  
7>         - admin  

My 'manage.py' is @ 1> so I understand that I have to make the edits in the following files&locations.

@ 2> the settings.py
@ 2> the urls.py

However:
A. I currently have no 'view.py' file @ 2>, would this be a new file? After following the tutorial I currently have a 'view.py' @ 3>;
B. Should the '404.html' go into a new templates directory under @2> or one of the two existing template directories (@4> or @6>)? I assume that a custom '500.html' would go into the same directory.

I would be happy to post any more of my code you want to look at.

pnuts
  • 58,317
  • 11
  • 87
  • 139
SeeGull
  • 177
  • 1
  • 2
  • 8
  • 1
    possible duplicate of [What could cause a Django error when debug=False that isn't there when debug=True](http://stackoverflow.com/questions/4970489/what-could-cause-a-django-error-when-debug-false-that-isnt-there-when-debug-tru) – Zdeslav Vojkovic Mar 19 '13 at 12:10
  • sadly I am having the problem localy on the development server. I am nowhere near a production environment. – SeeGull Mar 19 '13 at 12:54

5 Answers5

10

Most likely, you forgot to set ALLOWED_HOSTS in settings file.

Zdeslav Vojkovic
  • 14,391
  • 32
  • 45
  • 9
    sadly setting 'ALLOWED_HOSTS = ['localhost', '127.0.0.1']' in the settings.py has no effect. I also tried 'ALLOWED_HOSTS = '*' with no benefit. – SeeGull Mar 19 '13 at 12:49
  • AFAIK missing `ALLOWED_HOSTS` should result in 400 (Bad Request) errors, not 500 (Internal Server Error). This shouldn't be the cause—at least not the *root* cause—of the problem. – uranusjr May 10 '14 at 15:57
  • 1
    I did the same thing as SeeGull and still receive the error. –  Oct 15 '15 at 21:47
5

I had the same issue before, I was able to fix this by creating 404.html in my template directory, specifying valid ALLOWED_HOSTS and restarted my webserver

helloworld2013
  • 3,094
  • 5
  • 19
  • 26
5

I had a similar issue and was able to fix it with this minimal amount of code, assuming your django project is named mysite:

# mysite/mysite/settings.py
DEBUG = False
ALLOWED_HOSTS = ['localhost']
# Tells your project to find your custom 404.html at mysite/mysite/templates/404.html
INSTALLED_APPS = (..., 'tutorial', ...)

# mysite/mysite/urls.py
handler404 = 'tutorial.views.custom_404'

# mysite/mysite/views.py
from django.shortcuts import render

def custom_404(request):
    return render(request, '404.html', {}, status=404)

# mysite/mysite/templates/404.html
<h1>CUSTOM 404 PAGE</h1>

Don't forget to restart your server. =P


Directory structure should look like:

mysite  
    - mysite
        - settings.py
        - urls.py
        - views.py
        - templates
            - 404.html  
    - polls  
        - templates  
            - polls
epylinkn
  • 1,128
  • 12
  • 16
  • Do you put handler404 = 'tutorial.views.custom_404' inside or outside of urlpatterns? And what is "tutorial" in your answer - I don't see it in your directory structure. – user2121620 Aug 11 '14 at 20:00
0

Try running the server like this

python manage.py runserver localhost:8000

If you want it work from all addresses

python manage.py runserver 0.0.0.0:8000

Vineet Markan
  • 63
  • 1
  • 10
0

Here's what worked in my case:

  • I just added a 404.html in my templates
  • I also did python manage.py collectstatic --noinput

And it solved the error for me.

Suraj S Jain
  • 515
  • 3
  • 10
  • 24