0

Long story short, I created a django project and used the django for professionals book 2.2 as a reference(Made a couple more fields in the models). However, once I reached the security chapter and tried running the docker commands in the terminal, I get a programming error.

After researching and trying to debug, I decided to delete all migrations from the project, the volume from the docker container, and delete/comment out certain files/code.

From here, I reran all the migrations and now when I check my terminal, I still get 21 unapplied migrations and "Starting development server at http://0.0.0.0:8000/". When I click on the link, I get the "Disallowed Host at / ...Invalid HTTP_HOST header:..."

I am just wondering what went wrong because it seems to me that my docker-compose.yml and settings.py seems to be contributing to this error. I am trying to get my terminal to get the link to "https://127.0.0.1:8000/".

settings.py: I am using the postgres database

DEBUG = int(os.environ.get('DEBUG', default=0))

ALLOWED_HOSTS = []

docker-compose.yml: I left out the secret key

version: '3.8'

services:
  web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    environment:
      - DEBUG=1
      - ENVIRONMENT=development
    volumes:
      - .:/code
    ports:
      - 8000:8000
    depends_on:
      - db

  db:
    image: postgres:12.3
    volumes:
      - postgres_data:/var/lib/postgresql/data/

volumes:
  postgres_data:

Sidenote: Prior to deleting the programming error, and deleting the migrations, I was able to get the 127.0.0.1:8000 to work even though I had 0.0.0.0:8000 in my docker-compose.yml.

Any help is greatly appreciated.

SL42
  • 201
  • 3
  • 17
  • try to add it to allowed hosts, like , ALLOWED_HOSTS = ['localhost', ' 0.0.0.0'] – taha maatof Dec 09 '20 at 05:12
  • *"I was able to get the 127.0.0.1:8000 to work even though I had 0.0.0.0:8000 in my docker-compose.yml."* - That is not surprising. 0.0.0.0 means listen on all available IP addresses. 127.0.0.1 is one of them ... unless there is something seriously straing about your networking. – Stephen C Dec 09 '20 at 05:17
  • @StephenC, thank you for the response. I am assuming that there is something strange about my networking. – SL42 Dec 09 '20 at 13:21
  • If you think the problem is your networking, this is the wrong place to ask. Try https://serverfault.com/ or maybe https://unix.stackexchange.com/ – Stephen C Dec 09 '20 at 13:31
  • @tahamaatof, I tried the following allowed hosts and unfortunately, it does not work. – SL42 Dec 09 '20 at 14:55
  • Prior to the issue, did you have `https` working ("https://127.0.0.1:8000/") or was it `http`? ("http://127.0.0.1:8000/")? – Ben Dec 09 '20 at 20:35
  • not "exactly" a duplicate but your answer can more or less be found here https://stackoverflow.com/questions/8023126/how-can-i-test-https-connections-with-django-as-easily-as-i-can-non-https-connec – Display name Dec 10 '20 at 01:04
  • @Ben, I had https working("127.0.0.1:8000/") working. – SL42 Dec 10 '20 at 20:49

3 Answers3

0

Ok. I was able to figure out why I was not able to get https://127.0.0.1:8000 to work in django. There was nothing wrong with the code, but all I had to do was refresh the cache on my browser(In this case, it was firefox).

SL42
  • 201
  • 3
  • 17
0

I just restarted the server and it worked: python manage.py runserver

Jonathan Ciapetti
  • 1,261
  • 3
  • 11
  • 16
FlyHeavies
  • 29
  • 1
  • 3
-1

if you want to serve your site on https port only with python web service you can use https://github.com/teddziuba/django-sslserver

but best practice is to use django + wsgi + Nginx

on docker image you start your project with any wsgi script like(wsgi or gunicorn)

wsgi script handle workers for your project.

then request handling done with Nginx and https certificates handled with Nginx and your Django project runs on http port or linux socket file only.

vorujack
  • 1,778
  • 16
  • 22