changes to settings.py not updating - django

I am trying to get a django app to work on ubuntu with apache.
I am getting the error message "You may need to add 'x.x.x.x' to ALLOWED_HOSTS."
I have made the changes to settings.py, but still get the error message. I can see in the debug output that it looks like the changes I made to settings.py are not taking effect. I have tried restarting apache ( /etc/init.d/apache2 restart) but still no help.
Any input would be greatly appreciated.
thanks!
Bob

Do you have ALLOWED_HOSTS defined 2 times in settings.py ? It should be defined only 1 time.

Are you managing your project files on the Ubuntu server directly or via FTP?

Just add sudo before when running server script using python and it works like a charm. ex
sudo python3 manage.py runserver 0.0.0.0:8000

Related

Django migrate works but doesn't reflect on live site

I made some updates to a project: add 1 admin model, add 1 template
I'm using wagtail. I pulled the updates to my server, ran migrations, got success. I restart nginx and gunicorn, I even rebooted the server.
When I go to the wagtail admin, my adminmodel is missing (it exists locally). when I go to create a new page, my template is available, but when I select it I get taken to a wagtail 404 page.
Ubuntu 20.04
ngnix
gunicorn
django/wagtail
digital ocean vpc
digital ocean postgres database cluster
The site works like normal, only a template is available I can't select, and the Model, that migrated, isn't available and isn't showing up in admin. my local version is working perfectly, with no differences. it seems like the server is both updating and not updating. I don't get it. running makemigrations or migrate returns no changes. even when running on the specific app. Do I need to do something to rebot the database?
Listen it looks like caching issue with Nginx. Try clearing the cache.
I have 2 settings files: dev.py, production.py
dev.py is connected to a sqlite3 db and production is connected to the digitalocean postgres cluster.
python manage.py automatically uses dev.py (at least in my setup) so my migrations were working, but they were targeting an old copy of the sqlite.db I had on the server, or perhaps created one as it does when one doesn't exist. Either way, the live site runs on the production.py settings and this is why the changes were being made but not reflecting.
The correct way to run migrations when you have a production.py file is similar to:
python manage.py migrate --settings=<settings app>.<settings folder>.production
I'm also going to add that I found that you need to collectstatic each time you update your css stylesheet. that is a little tedious, and maybe I am missing an automation step, but considering that is the case, just to be safe, I run like this:
python manage.py collectstatic --settings=<settings app>.<settings folder>.production
It doesn't cause any issues and it works, so I use the --settings flag to be safe.
git pull
python manage.py migrate --settings=app.settings.production
python manage.py collectstatic --settings=app.settings.production
sudo systemctl restart ngix
sudo systemctl restart gunicorn
I don't know that restarting ngix is necessary except it solved something with my static files updating I believe.

Gunicorn is not seeing new settings

I updated one environment variable inside settings.py and restarted gunicorn, but it is seeing the old settings. Here is the command I use to start it:
nohup /opt/my_proj/.virtualenvs/my_proj/bin/python2 /opt/my_proj/.virtualenvs/my_proj/bin/gunicorn --bind=0.0.0.0:8008 --timeout=1800 --log-level=debug --error-logfile=/opt/my_proj/gunicorn_nik.log --enable-stdio-inheritance --capture-output --user=me --pythonpath=/opt/me/code/my_proj,/opt/me/code/my_proj/seqr_settings wsgi &
I printed out paths to make sure that the scripts are running under my 'my_proj' directory and also looked up in 'gunicorn_nik.log' verifying that there I see it pointing to 'my_proj' folder. Then I removed settings.py to make sure still that it is the file gunicorn is picking up. The startup failed. I tried modifying settings.py printing out something from it but it is not working, logger.info is not printing from there.
I have several Django projects on one cluster node running (not sure that it is important).
Its as if Django is storing some cached file and using it, but how come gunicorn restart does not fix it? Seems weird to me. Any suggestions would be greatly appreciated.
The issue was that in .bashrc an environment variable was set that was somehow overwriting the value in settings.py that was having the same name.

How to use Django logging with gunicorn

I have a Django 1.6 site running with gunicorn, managed by supervisor. During tests and runserver I have logging on the console, but with gunicorn the statements don't show up anywhere (not even ERROR level logs). They should be in /var/log/supervisor/foo-stderr---supervisor-51QcIl.log but they're not. I have celery running on a different machine using supervisor and its debug statements show up fine in its supervisor error file.
Edit:
Running gunicorn in the foreground shows that none of my error messages are being logged to stderr like they are when running manage.py. This is definitely a gunicorn problem and not a supervisor problem.
I got a response on GitHub:
https://github.com/benoitc/gunicorn/issues/708
Since you have passed disable_existing_loggers the Gunicorn loggers are disabled when Django loads your logging configuration. If you are setting this because you want to disable some default Django logging configuration, make sure you add back the gunicorn loggers, gunicorn.error and gunicorn.access with whatever handlers you want.
In /etc/supervisor/conf.d/your-app.confyou should set log paths:
stdout_logfile=/var/log/your-app.log
stderr_logfile=/var/log/your-app.log
First, in your supervisor config for the gunicorn script, be sure to define
stdout_logfile=/path/to/logfile.log
redirect_stderr=true
That will make stdout and stderr go to the same file.
Now, on your gunicorn script, be sure to call the process with the following argument
gunicorn YourWSGIModule:app --log-level=critical

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.

Django manage.py runserver never finishes "validating models"

I am trying to follow the django tutorial. I am running on windows+eclipse.
When I run python manage.py runserver I get the message Validating models... and afterwards see no progress...
Am I doing something wrong?
I've got my answer in another question: can't get django to work in eclipse + windows
When I ran the server with the --noreload option, I saw that there's an exception thrown. After I fixed that, the output does complete, and says:
Validating models...
0 errors found
Django version 1.2.1, using settings 'XXX'
Development server is running at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
The shell is running the runserver process, and it won't go back to a command prompt until the server process ends. So, this sounds normal.
Is the server functioning?
You could possibly be importing something in your models.py file that is failing in another file. I had an import in a try/catch clause that caused this behavior. Installing the missing dependency fixed the problem.
This is what you're supposed to see. If you make a request to your test site (via your browser), you will see the log of the request. If you don't see that, then something else is wrong.