【发布时间】:2018-05-20 10:21:11
【问题描述】:
我想使用本地 IP 在本地运行 Django 服务器。
我在此处映射了localhost:
$ head -n 1 /etc/hosts
127.0.0.1 localhost
我的settings.py 中有这段代码:
import os
ALLOWED_HOSTS = ['HERE.IS.MY.IP', 'localhost', '127.0.0.1']
print "ALLOWED_HOSTS: {}".format(ALLOWED_HOSTS)
在我的 mysql 数据库中,我有这个站点表:
mysql> select * from django_site;
+----+--------------------+----------------+
| id | domain | name |
+----+--------------------+----------------+
| 1 | example.com | example.com |
| 5 | HERE.IS.MY.IP:8000 | projectsdb |
| 8 | 127.0.0.1:8000 | projectsdb |
| 9 | localhost:8000 | projectsdb |
+----+--------------------+----------------+
我在127.0.0.1本地运行服务器:
$ python manage.py runserver 8000
ALLOWED_HOSTS: ['HERE.IS.MY.IP', 'localhost', '127.0.0.1']
Performing system checks...
# ...
Django version 1.10.5, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
如果我去这个地址http://HERE.IS.MY.IP:8000 它可以工作。但我当然想使用http://localhost:8000 或http://127.0.0.1:8000 等本地IP 打开它。但这不起作用。我错过了什么?
【问题讨论】:
-
这应该可以。会发生什么?
-
你可以试试这样吗:
ALLOWED_HOSTS = ['*'] -
我在我的
settings.py中使用了ALLOWED_HOSTS = ['*'],现在http://HERE.IS.MY.IP:8000、http://localhost:8000和http://127.0.0.1:8000都不起作用。 -
您的
/etc/hosts是完全默认的还是您编辑过的?访问 localhost:8000 时收到什么错误消息? -
@PeteTinkler 我从未编辑过我的
/etc/hosts