Seeing Django debug 404 page despite having DEBUG = False - django

I have been following this tutorial along to deploy my first Django site and have successfully reached the section 'Configure Nginx to Proxy Pass to Gunicorn' which all seems to be working.
My problem is that, despite my settings.py file containing the following:
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
I am still getting Django's debug=true 404 page with the following error:
"You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page."
I changed the file to DEBUG = False after completing the 'Configure Nginx to Proxy Pass to Gunicorn' step in the tutorial by pulling the change from my GitHub repository. Am I missing an additional step with Nginx in order to turn debug off and serve a standard 404 page?
Edit: It actually seems that any adjustments I make to the settings.py file in my repository, when pulled on to the server, don't have any effect. I commented out the whole settings.py file to see if it would break the webpage; nothing happened.

I'm guess that you does't restart web server after code editing. Try to restart your Gunicorn server, when you work with django runserver command it automatically restart web server on any change in code. Gunicorn dont do this by default, if you wanted that Gunicorn also restart server automatically, run it with --reload argument. But it dose not recommended into production

Related

gunicorn Serve Error(500) in django 3.2 app on localhost

I'm trying to serve my Django 3.2 application using gunicorn on my localhost. First time, it ran correctly. Later, I changed DEBUG parameter to False in settings.py and I run it again. This time it gives me a server error. In terminal there is no error. See the pictures below. Why is this happening ? How to fix this ?
Error Page
settings.py
terminal
As you are trying to use it in your localhost you need to change the DEBUG = True instead of DEBUG = False and It will work smoothly

Django won't start on apache webserver

I'm trying to run my django project with an apache webserver.
I get the following error:
Errors : The included urlconf hastahane.urls doesn't have any patterns in it`
If I'm running my project with the django development server there isn't any error.
Do you have an idea?
You should check that the DEBUG settings is set to False and that you have properly filled the ALLOWED_HOSTS list (see the docs).
Also, are you using the django-debug-toolbar? In that case that may be the cause of the error. Check this answer.

Django is giving me a 404 error

I installed Django, and it works. I set it up so it uses my mysql database, and I started a project. So far so good.
I followed the tutorial on setting up your first Django app over at
https://docs.djangoproject.com/en/dev/intro/tutorial01/
It is a tutorial over setting up a pre-existing poll app where everything has practically been built for you. The database structure has even been handled.
I ran:
python manage.py startapp polls
python manage.py sql polls
python manage.py syncdb
I didn't receive any kind of success message so I went into my phpmyadmin, and hooray! There are new tables and rows in my database.
Their tutorial then told me to run:
python manage.py shell
and that I'd see some database stuff, but I didn't. Why could this be? I ignored it and went on to step two. I still hadn't set DEBUG in my settings.py to False so I did. Only to get a 500 error.
After some digging I read I needed to add:
ALLOWED_HOSTS = ['my ip address'];
I did this and now after running:
python manage.py runserver myip:8000
When I try to access Django in my browser I get a
Not Found
The requested URL / was not found on this server.
Obviously / changes to a different location when navigating to those places as well, but the point is I get a 404 no matter what.
So I look at my terminal and I have a yellow message in my terminal that says.
"GET / HTTP/1.1" 404 74
and there is 1 message like this for each place I tried to access.
I'm thinking there is a Python package that I don't have installed on my server?
I do not want to use ALLOWED_HOSTS ['*'] I read that this is bad practice. I did try it and it produces the same results as using my ip address in place of the * (I just wanted to add that extra piece of info in case it helps)
If you want to use the database shell, you should run the dbshell command instead of shell as in your post, like this:
python manage.py dbshell
If you run shell, you get a Python shell, where you can easily import and inspect the Python objects of your project.
On your local PC, it's better to have DEBUG = True in your settings.py. That way you don't need to bother about ALLOWED_HOSTS, because in debug mode all hosts are allowed. Secondly, when you get a 404 error in debug mode, the page will show you the valid URLs that you can try.
The Django tutorial certainly works. The only way it won't work for you is if you missed a step or mistyped something somewhere. If you start over and pay extra attention, I think it will work.

Why does setting debug to false in django settings stop Heroku app from loading in development and production?

In my settings i have set DEBUG=False but this instead generated a 500 Error in both dev and production. so i looked around and came across this (Setting DEBUG = False causes 500 Error) and tried it out.
ALLOWED_HOSTS = ['www.heroku.com']
But this did not work, what am i not doing right? Am hosting with heroku
Your app is not hosted on www.heroku.com. Instead, try
ALLOWED_HOSTS = [".herokuapp.com"]
did you try
python manage.py collecstatic
?
you should review files directory becuase in prodhuction static files directory changes. heroku uses whitenouse

Running Gunicorn with Foreman

If I do runserver or gunicorn straight from the commandline, the website works fine. However, if I try to run gunicorn using Foreman as specified in the Heroku documentation:
web: gunicorn myapp.wsgi
Then my website's static files are suddenly inaccessible; trying to go to
http://0.0.0.0:5000/static/js/jquery-1.9.1.min.js
Only gives me this error message:
Using the URLconf defined in myapp.urls, Django tried these URL patterns, in this order:
^$ [name='homepage']
...
The current URL, static/js/jquery-1.9.1.min.js, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
As I've said, the static files are definitely accessible if calling gunicorn or runserver directly, or even if using runserver with foreman start, so I'm guessing it doesn't have too much to do with my static files settings?
Also, running gunicorn on foreman with DEBUG = False doesn't even return anything except for a 500 error. Nothing is logged, not even on Sentry, so I have no idea what's wrong. It also works fine using gunicorn or runserver alone, or using runserver with foreman.
Any ideas what I can do to fix this problem?
Don't know about why the static files were not being retrieved, but the DEBUG = False 500 error was simply caused by my ALLOWED_HOSTS setting, and fixing that produced no further issues.
I would try to print out settings while running under gunicorn and see if your media URL is correct and js files are indeed accessible.
As for 500 error it is probably trying to show you 404 page, but you do not have a template for it, so it raises 500, and old django quirk ;)
PS. This all is assuming that you verified that you have correct URL pattern in that error