Should server IP address be in ALLOWED_HOSTS django setting?

后端 未结 3 1552
栀梦
栀梦 2021-02-05 00:21

Since upgrading to django 1.5 my logs show several SuspiciousOperation exceptions with the text:

Invalid HTTP_HOST header (you may need to set ALLOW         


        
3条回答
  •  囚心锁ツ
    2021-02-05 01:10

    In practice, just edit the file MyProjectName/settings.py and add the host IP (IP address to the machine in which you're running Django) to the list ALLOWED_HOSTS, which by default is empty.

    So, in your, case we would have before the changes:

    ...
    ALLOWED_HOSTS = []
    ...
    

    After changes:

    ...
    ALLOWED_HOSTS = ['168.62.208.14'] #Make sure your host IP is a string
    ...
    

    Run the server again and you should be good. Here's an example using the port 8000:

    python manage.py runserver 168.62.208.14:8000

    . Now if you go to your browser and enter the address http://168.62.208.14:8000, you should find yourself in the page "Congratulations on your first Django-powered page".

提交回复
热议问题