170

I am trying to develop a website using Django framework and launched using DigitalOcean.com and deployed the necessary files into django-project.

I had to include static files into Django-project and After collecting static files, I tried to refresh my ip

I am including the tutorials which I have used to create the website. https://www.pythonprogramming.net/django-web-server-publish-tutorial/

I am getting the following error :

DisallowedHost at / Invalid HTTP_HOST header: '198.211.99.20'. You may need to add u'198.211.99.20' to ALLOWED_HOSTS.

Can somebody help me to fix this ? This is my first website using Django framework.

2
  • That may also be your Floating IP address on DigitalOcean. Oct 10, 2018 at 17:04
  • Can I use my external IP for starting the server? Sep 17, 2020 at 8:36

5 Answers 5

281

The error log is straightforward. As it suggested,You need to add 198.211.99.20 to your ALLOWED_HOSTS setting.

In your project settings.py file,set ALLOWED_HOSTS like this :

ALLOWED_HOSTS = ['198.211.99.20', 'localhost', '127.0.0.1']

For further reading read from here.

12
  • Now getting the following error Request URL: 198.211.99.20 Exception Type:TemplateDoesNotExist Exception Value: personal/home.html Exception Location: /usr/local/lib/python2.7/dist-packages/django/temp‌​late/loader.py in get_template, line 25 I have my templates in the following folder /home/django/django_project/personal/templates/personal Nov 14, 2016 at 6:49
  • @Kathir There are many examples of that error.Just google it and if the problem still continues,then please ask it as a separate question,comments are not very descriptive. Nov 14, 2016 at 6:52
  • 3
    This definitely works but is bad practice as you should always use a .env file
    – Abhishek J
    Mar 7, 2017 at 17:38
  • 2
    @JesusAlmaral A .env file is a local file containing passwords and other sensitive information. If you put all this sensitive information inside your code itself then it could get compromised. Thus we use this local file .env and everyone stores with their own passwords etc locally
    – Abhishek J
    Nov 1, 2017 at 4:57
  • 1
    FWIW, in our case, all accesses by the naked IP address are dubious. Our solution was to configure the web server to reject all Host headers not matching the ones Django accepts. Jun 1, 2022 at 6:30
7

settings.py

ALLOWED_HOSTS = ['*'] // if you are in dev or docker

Edited

Ok guys, dont do this in production if you are not using docker, just put the IP addr.

Grettings

5
  • 4
    As pydanny said "...don't leave it as such once you get this figured out. The reason is that makes Django potentially vulnerable to HTTP_HOST header attacks. And automated scripts scour the internet to check if sites have this vulnerability." github.com/pydanny/cookiecutter-django/issues/…
    – pyjavo
    May 24, 2018 at 1:21
  • 7
    Lol, that is for development env. in production only need set DEBUG=False. May 30, 2018 at 15:10
  • 13
    You do not want to use '*' for production. This completely bypasses the reason and security of the allowed hosts. Jun 8, 2018 at 15:17
  • 1
    @AndyPoquette generally you're right, but using docker (and not exposing the backend's port, but using a reverse proxy like nginx) it's OK to use '*' even for production.
    – artem
    Nov 3, 2021 at 20:38
  • 1
    Never, NEVER do this! Allowed * is a primary security error. Only do this in development mode! Dec 1, 2021 at 19:30
2

In your project settings.py file,set ALLOWED_HOSTS like this :

ALLOWED_HOSTS = ['62.63.141.41', 'namjoosadr.com']

and then restart your apache. in ubuntu:

/etc/init.d/apache2 restart
0

if no other answer work you can try modifying manage.py and add this three lines

from django.utils.regex_helper import _lazy_re_compile
import django.http.request
django.http.request.host_validation_re = _lazy_re_compile(r"[a-zA-z0-9.:]*")

to end up having something like this:

import os
import sys

from django.utils.regex_helper import _lazy_re_compile
import django.http.request    
django.http.request.host_validation_re = _lazy_re_compile(r"[a-zA-z0-9.:]*")

def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project01.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

as it is explained in this post: How to Solve "The domain name provided is not valid according to RFC 1034/1035" in Django during Development

0

You can add ALLOWED_HOSTS to your settings file or env file:

ALLOWED_HOST = [".localhost", "127.0.0.1", "[::1]"]

Not the answer you're looking for? Browse other questions tagged or ask your own question.