i sent my python3 django files to digital ocean server and getting 502 bad gateway error. I tried all the tips given elsewhere in stackoverflow but none worked. I believe there is something wrong with my settings.py. Specifically below lines, please let me know your suggestions:
ALLOWED_HOSTS = ['*']
# Find out what the IP addresses are at run time
# This is necessary because otherwise Gunicorn will reject the connections
def ip_addresses():
ip_list = []
for interface in netifaces.interfaces():
addrs = netifaces.ifaddresses(interface)
for x in (netifaces.AF_INET, netifaces.AF_INET6):
if x in addrs:
ip_list.append(addrs[x][0]['addr'])
return ip_list
# Discover our IP address
ALLOWED_HOSTS += ip_addresses()
Bad Gateway error sometimes comes even when there is an error in codes. It can be due to python version. Digital ocean uses 2.7, not 3.6. You have to upgrade Django to 1.11.
Make sure your directories inside \home\django_project\django_project are all well.
After all this in the ubuntu console by running python manage.py runserver like we used to do in local environment debug your errors. Don't look anywhere else, this is the best way and will work surely. I wasted 4 days in same error when I switched to production.
Good morning,
My solution in settings.py was:
ALLOWEDHOSTS = ['domainname', 'droplet_IP']
and add the bottom of settings.py
change:
ALLOWEDHOSTS = ipaddresses()
to
ALLOWEDHOSTS += ipaddresses()
Works in Feb '18.
Related
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
I want to play around with social-django authentication app. Iwant to add for login with facebook. For that I need to change my 127.0.0.1 to something like my-site.com. I wanted to change /etc/hosts, unfortunetely it doesn't exist.
I created hosts file using touch command and added 127.0.0.1 my-site.com (nothing more) and restarted my computer. I tried runserver command and copy-pasted hostname above to the beggining of the link (my-site.com:8000) but it didn't work.
My django project runs on venv.
If you have any ideas on solving my problem, please share
(I've posted a similar question on superuser.com, but no one seemed to know a solution there, so I ask here)
EDIT
my settings py now look like this
ALLOWED_HOSTS = ['my-site.com', '127.0.0.1']
But it still doesn't work
You can use localhost instead of 127.0.0.1 as one of the workaround.
Use this script:
$ python manage.py runserver localhost:8000
if you are on Mac or linux :
edit your /etc/hosts/ file and add
127.0.0.1 mysite.com
if you are on window:
Go to C:\Windows\System32\Drivers\etc\hosts and add
127.0.0.1 mysite.com
I am not sure about the mac and linux one but for windows it worked, hopefully it will work for others too.
You can use LocalTunnel ,it allows you to easily share a web service on your local development machine without messing with DNS and firewall settings.
Install Localtunnel globally (requires NodeJS) to make it accessible anywhere:
npm install -g localtunnel
Run django server using
python manage.py runserver
and use the command line interface to request a tunnel to your local server:
lt --port 8000
It will give you a temporary url to use in any place,You can use that url in other sysytems too,the url will be live until the server is running in your system.
You can also use the temporary url in facebook setting for testing purpose.
You need to change the file settings.py so that my-site.com is contained in the list of ALLOWED_HOSTS.
For security reasons Django needs to know what server names it servers, which is specified by ALLOWED_HOSTS. On Django's web site it states that:
A list of strings representing the host/domain names that this Django site can serve. This is a security measure to prevent HTTP Host header attacks, which are possible even under many seemingly-safe web server configurations.
Django Settings: ALLOWED_HOSTS
You need to buy, or get for free, a DNS (you can get a subdomain from Noip for free).
You then have to connect your domain to your router (search in the settings) and port forward your computer through the port you want. Now you will be able to visit "yourdomain.com" and you will get your django server.
Don't forget to add your domain to ALLOWED_HOSTS.
I wish it helps!
Before everything, you need to add your localhost domain to ALLOWED_HOSTS variable in settings.py:
ALLOWED_HOSTS = ['my-site.com', '127.0.0.1', 'localhost']
If you use PyCharm as your IDE, you can edit your configuration settings as so:
I posted a 7 step tutorial to achieve this with and without specifying the port, on a similar question for anyone still looking for a solution!
I am trying to use Cookiecutter on a Digital Ocean server. (Not using Docker)
I followed the direction to install on Ubuntu 16 with Django, Postgres and Gunicorn. https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
I can not get past the allowed host error.
DisallowedHost at /
Invalid HTTP_HOST header: '128.199.100.100:8000'.
You may need to add '128.199.100.100' to ALLOWED_HOSTS
I have the setting in production.py
ALLOWED_HOSTS = env.list ( 'DJANGO_ALLOWED_HOSTS',
default = [ '128.199.100.100' ] )
Do I need to change any setting to make it a production environment?
The only documentation on the Cookiecutter site is for pythonAnywere and Docker. http://cookiecutter-django.readthedocs.io/en/latest/deployment-on-pythonanywhere.html
I just want a simple DO install. Can not find any documentation?
Thank you.
At this stage, you need to add '128.199.100.100:8000' (including the port) to your ALLOWED_HOSTS. You could set it as an environment variable in the runserver command:
ALLOWED_HOSTS = env.list ( 'DJANGO_ALLOWED_HOSTS',
default = [ '128.199.100.100', '128.199.100.100:8000' ] )
Or you could temporarily add it to the default in your settings.
DJANGO_ALLOWED_HOSTS=128.199.100.100:8000 ~/myproject/manage.py runserver 0.0.0.0:8000
Eventually, the tutorial will change gunicorn to use a socket file and you will access the website on port 80 with Nginx, so you won't need '128.199.100.100:8000' in your ALLOWED_HOSTS.
I had the similar problem and I believe I found a solution.
If you are using environmental variables that passes DJANGO_ALLOWED_HOST value (the most secure way of copying credentials when deploying).
Then look closely at the syntax where you are defining the list of IP addresses allowed for the host. The syntax of environmental variables is completely different from django environmental variables and that made me confused.
At first I defined DJANGO_ALLOWED_HOSTS using python syntax
export DJANGO_ALLOWED_HOSTS="['localhost', '127.0.0.1', '192.168.1.110']"
Which is completely incorrect as looking down the error message trace under Settings section, I got the following ALLOWED_HOSTS value
ALLOWED_HOSTS ["['localhost',", "' 127.0.0.1'"," ' 192.168.1.110']"
Which is just means the variables get parsed assuming completely different syntax. Making django settings into comma-separated string also didn't fix the issue
export DJANGO_ALLOWED_HOSTS="localhost, 127.0.0.1, 192.168.1.110"
The parsed result is the following, notice space character leading in the second and third element.
ALLOWED_HOSTS ['localhost', ' 127.0.0.1', ' 192.168.1.110']
It seems that the logic that parses address values is very particular and the only correct way of listing several hostsusing only comma as a separator.
export DJANGO_ALLOWED_HOSTS="localhost,127.0.0.1,192.168.1.110"
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.
I'm on Windows XP with the latest install of Python 2.6 (and the development server has been working up until last night). I have the path and Python path stuff all set up, and my dev server has worked forever. I recently replaced my django-trunk with a new pull from the Django trunk. I thought maybe there was an import error or something that Django wouldn't catch in one of my app's models.py so I started a new project (empty but just for testing) and it still didn't work. I restarted my computer and tried the new empty app again python manage.py runserver 8080 and went to http://127.0.0.1:8080/ and it worked ("Congrats. Django is insta..."). So I CD over to my real project and tried again and it didn't work. I'm not getting a stack trace or anything like that. I either get [17/Ja/2010 16:30:51] "GET / HTTP/1.1" 301 0 as output when I visit http://127.0.0.1:8080/ in my CMD prompt or I get nothing (even if I hard refresh, etc). What could this be?
Update (Important):
Firefox tells me Firefox can't find the server at www.127.0.0.1. even though I'm at http://127.0.0.1:8080/. Does this mean that Django is really sending a 301 to www.127.0.0.1 for some other reason?
I removed PREPEND_WWW from settings.py, and even removed all the apps (except for the django admin and preset ones) that were installed in settings.py.
Update 2: It works in Safari! How can this be? It's like Firefox is getting some sort of 301 but Safari works just fine.
yep, 301 permanent redirect is remembered by firefox, i've been stuck once on that one, restarting or cleaning history/cache didn't help, so i just ran it on another port.
edit after commenting:
assuming you use some localhost_settings.py to setup your project locally and still want to www_redirect on the production website:
try:
from localhost_settings import *
PREPEND_WWW = False
except ImportError:
PREPEND_WWW = True
i do it this way