How to host a django website in my pc and view it in another pc?

一个人想着一个人 提交于 2020-01-12 10:17:31

问题


I have a django project in my pc.

In terminal I've run python3 manage.py runserver <my ipaddress>:8001

When I try to open the link in another pc, it is showing error page which says:

Invalid HTTP_HOST header: '<my ipaddress>:8001'. You may need to add '<my ipaddress>' to ALLOWED_HOSTS.

What should I do?

And moreover is it possible to put some text in place of ipaddress in the url? For example, I want to host it as myproject/ instead of that complex url.


回答1:


On one condition this will work

if both computers are on the same network like local Hotspot or same LAN network

steps:

  1. add '*' in your django projects's setting file in Allowed Host it will look like

ALLOWED_HOSTS = ['*']

  1. run your server on this ip 0.0.0.0 and port any like 8000 using this command

manage.py runserver 0.0.0.0:8000

  1. run ifconfig if you are using linux ipconfig if windows then you will get your ip address of your server

  2. Open browser in another computer and enter the ip of server shown in 3rd step with port as 8000

http://ip-of-server:8000




回答2:


Instead of passing <my-ip-address> to the runserver command, pass 0.0.0.0.




回答3:


If both the machines are in the same network you can run the application on 0.0.0.0 IP address (refers to all IPv4 addresses on the local machine). Refer this link wiki 0.0.0.0 for more details. So, on application server run this:

manage.py runserver 0.0.0.0:8001

Now, from the other machines, access it using http://youripaddresss:8001 , where < youripaddress > is the actual ip address of your machine.



来源:https://stackoverflow.com/questions/51298664/how-to-host-a-django-website-in-my-pc-and-view-it-in-another-pc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!