Albert
Albert

Reputation: 143

How can I change django runserver url?

I'm trying to change django project url, so that users, who wants to connect to website in local area network will see url instead of localhost:8000 or 127.0.0.1. I need to change localhost:8000/users/board to be http://example.eu. I've tried to python manage.py runserver http://example.euand then thought about changing url's but it doesn't work. Also tried to change hosts file(Windows OS), but as far as I understand I need real ip address not url. Is it possible to do this thing and how?

Upvotes: 4

Views: 10677

Answers (2)

Vasco
Vasco

Reputation: 911

Run a local domain with the same port

  1. Opening the /etc/hosts file on your mac with
sudo nano /etc/hosts

And for windows I believe you need to open:

C:\Windows\System32\Drivers\etc\hosts
  1. In here add the domains you want, for example I added vazkir.com
....
127.0.0.1       localhost
127.0.0.1       vazkirtje.com
127.0.0.1       www.vazkirtje.com
.....
  1. Lastly you can add it the domain to you ALLOW_HOSTS in your settings.py:
ALLOWED_HOSTS = ["localhost", "127.0.0.1", "vazkirtje.com", "www.vazkirtje.com"]
  1. And now you can visit your Django application (with port 8000) at:
http://vazkirtje.com:8000

The solution is not perfect since you do have to specify the port you are using, but this does let you use a domain for your local Django application in a relatively easy manner;)

Run a local non-existent domain without specifying the port

If you do want to run it without specifying the port; so just vazkirtje.com then you can use port 80, because this is the default port for HTTP.

  1. Make sure the domain you are testing does not exist, since the domain lookup will first be done on existing domains. So check if you get a similar message to the one I got on chrome, when visiting the url:

enter image description here

  1. Now you can specify this port by adding port "80" to the "runserver" command. You only do need to use "sudo" to run the command at this port, since you need admin rights for this. So run:
sudo python manage.py runserver 80
  1. And now you should be able to access your Django application by visiting:
http://vazkirtje.com

Upvotes: 1

Ritesh Agrawal
Ritesh Agrawal

Reputation: 821

You can use python manage.py runserver 0.0.0.0:8000. 0.0.0.0 means all IPv4 addresses on the local machine. So the server can be reachable by 127.0.0.1 and your private ip address like 10.10.5.8. So now others can access the server using http://10.10.5.8:8000. You the runserver on port 80, so that port can be removed from the url (by default is 80).

But to use any domain instead of ip, you have to change the hosts file of all the clients using the server to add domain to ip address mapping. Alternatively you can configure local network server to map the particular url to your system ip.

Upvotes: 6

Related Questions