1

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.

diogenes
  • 1,865
  • 3
  • 24
  • 51

2 Answers2

2

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"
Alex Volkov
  • 2,812
  • 23
  • 27
0

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.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • I added both of the hosts and still getting the same error? should I just go ahead to the Nginx part or do I have to master this step first? – diogenes Jan 17 '18 at 11:46
  • Make sure you've restarted `runserver` if you still can't get it to work, then you could try moving on, since the final deployment won't port 8000 anyway. – Alasdair Jan 17 '18 at 11:50
  • @AlexVolkov I don’t understand what you’re asking. – Alasdair Apr 24 '19 at 22:35