简体   繁体   English

Django 不允许的主机

[英]Disallowed Host at Django

I'm just starting out with Django for the first time.我是第一次开始使用 Django。 So I create a django project and run the command所以我创建了一个 django 项目并运行命令

python3  manage.py  runserver  0.0.0.0:8000

Instead of getting the expected django homepage, I get the following error message,我收到以下错误消息,而不是获得预期的 django 主页,

DisallowedHost at /
Invalid HTTP_HOST header: '0.0.0.0:8000'. You may need to add '0.0.0.0' to ALLOWED_HOSTS.
Request Method: GET
Request URL:    http://0.0.0.0:8000/
Django Version: 1.10.5
Exception Type: DisallowedHost
Exception Value:    
Invalid HTTP_HOST header: '0.0.0.0:8000'. You may need to add '0.0.0.0' to ALLOWED_HOSTS.
Exception Location: /usr/local/lib/python3.5/site-packages/django/http/request.py in get_host, line 113
Python Executable:  /usr/local/opt/python3/bin/python3.5
Python Version: 3.5.2

I haven't yet scratched the surface of django, so would appreciate some help as to how to fix this?我还没有触及 django 的表面,所以希望得到一些关于如何解决这个问题的帮助?

literally as the error suggested!字面上的错误建议! go ahead and add the line继续添加该行

0.0.0.0 to the ALLOWED_HOSTS in your settings.py 0.0.0.0settings.pyALLOWED_HOSTS

The error info is quite explicit.错误信息非常明确。 It should solve the problem right away.它应该立即解决问题。

I too had problems with this and it is not easy for a beginner to fix.我也遇到了这个问题,初学者很难解决。 Here below is the sequence required:以下是所需的顺序:

First find your settings.py file, which is located:首先找到您的 settings.py 文件,该文件位于:

projectName/projectName/settings.py项目名称/项目名称/settings.py

You can also find it using: find -name "settings.py" - likely you will have only one!您也可以使用以下命令找到它: find -name "settings.py" - 您可能只有一个!

Once you have found this, you need to open it in a text editor and change the line找到后,您需要在文本编辑器中打开它并更改行

ALLOWED_HOSTS =[ ] to ALLOWED_HOSTS =['your.host.ip.address'] - include the ' ' and the [ ] then save. ALLOWED_HOSTS =[ ] 到 ALLOWED_HOSTS =['your.host.ip.address'] - 包括 ' ' 和 [ ] 然后保存。 You will have to re-start the server to get this to work eg:您必须重新启动服务器才能使其正常工作,例如:

   python manage.py runserver your.host.ip.address:8000

If you get a syntax error, make sure you have something that looks like:如果您收到语法错误,请确保您有以下内容:

ALLOWED_HOSTS =['192.168.1.200'] or whatever your ip address is. ALLOWED_HOSTS =['192.168.1.200']或任何您的 IP 地址。

1- run 1-运行

ifconfig check you ip address, and you should add the ip to this lone: ALLOWED_HOSTS =['you IP address goes here'] ifconfig 检查你的 ip 地址,你应该将 ip 添加到这个单独的:ALLOWED_HOSTS =['你的 IP 地址在这里']

2- run: 2-运行:

python manage.py your-ip:8000 python manage.py your-ip:8000

3- go to your browser and check you ip:8000 3-转到您的浏览器并检查您的ip:8000

在此处输入图片说明

在此处输入图片说明

Just close the postman and try request in new tab.只需关闭邮递员并在新选项卡中尝试请求。 This worked for me.这对我有用。

1.You can also find it using: find -name "SETTINGS.py" - likely you will have only one! 1.您也可以使用以下命令找到它: find -name "SETTINGS.py" - 您可能只有一个!

2.Once you have found this, you need to open it in a text editor and change the line in the insetting File 2.一旦你找到这个,你需要在文本编辑器中打开它并更改插入文件中的行

 ALLOWED_HOSTS =['Your_compte_pythonanywher.pythonanywhere.com']

Additionally, you can have a file local_settings.py and settings.py .此外,您可以拥有一个文件local_settings.pysettings.py

If you are running in a local or a VPS server considered as local server:如果您在本地或被视为本地服务器的 VPS 服务器中运行:

python manage.py runserver 0.0.0.0:8001

You need to add in local_settings.py您需要添加local_settings.py

Also you can search how many ALLOWED_HOSTS has your code with:您也可以搜索有多少 ALLOWED_HOSTS 有您的代码:

grep -rin ALLOWED_HOSTS your_source_path

If you tried如果你试过

ALLOWED_HOSTS = ['*'] ALLOWED_HOSTS = ['*']

and didn't work while you have two manage.py file ( /manage.py and project_name/manage.py) inside the project.并且在项目中有两个 manage.py 文件(/manage.py 和 project_name/manage.py)时不起作用。

Try尝试

python project_name/manage.py runserver 0.0.0.0:8000 python project_name/manage.py runserver 0.0.0.0:8000

instead of代替

python project_name/manage.py runserver 0.0.0.0:8000 python project_name/manage.py runserver 0.0.0.0:8000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM