pawqo
pawqo

Reputation: 97

How to change 'localhost' url django

I want to play around with social-django authentication app. Iwant to add for login with facebook. For that I need to change my 127.0.0.1 to something like my-site.com. I wanted to change /etc/hosts, unfortunetely it doesn't exist.

I created hosts file using touch command and added 127.0.0.1 my-site.com (nothing more) and restarted my computer. I tried runserver command and copy-pasted hostname above to the beggining of the link (my-site.com:8000) but it didn't work.

My django project runs on venv.

If you have any ideas on solving my problem, please share

(I've posted a similar question on superuser.com, but no one seemed to know a solution there, so I ask here)

EDIT my settings py now look like this

ALLOWED_HOSTS = ['my-site.com', '127.0.0.1']

But it still doesn't work

Upvotes: 7

Views: 21171

Answers (7)

vishal vishwakarma
vishal vishwakarma

Reputation: 51

if you are on Mac or linux :

  • edit your /etc/hosts/ file and add

127.0.0.1 mysite.com

if you are on window:

  • Go to C:\Windows\System32\Drivers\etc\hosts and add

127.0.0.1 mysite.com


Then flush DNS pushing system changes

  • MacOS/Linux
sudo killall -HUP mDNSResponder
  • windows
  1. Open the Command Prompt as an administrator. You can do this by searching for "Command Prompt" in the Start menu, right-clicking on it, and selecting "Run as administrator".
  2. Type the following command and press Enter: ipconfig /flushdns

Then add to settings.py

ALLOWED_HOSTS = [‘mysite.com’]

Finally, start your django application and you should be able to access it. Note that you must access with port whatever it is, the port nowadays should be 8000 Hence full path would be mysite.com:8000

Upvotes: 3

Vasco
Vasco

Reputation: 911

I posted a 7 step tutorial to achieve this with and without specifying the port, on a similar question for anyone still looking for a solution!

Upvotes: 0

javiergarval
javiergarval

Reputation: 348

Before everything, you need to add your localhost domain to ALLOWED_HOSTS variable in settings.py:

ALLOWED_HOSTS = ['my-site.com', '127.0.0.1', 'localhost']

If you use PyCharm as your IDE, you can edit your configuration settings as so:

enter image description here

Upvotes: 0

Fantacy
Fantacy

Reputation: 83

You can use localhost instead of 127.0.0.1 as one of the workaround.

Use this script: $ python manage.py runserver localhost:8000

Upvotes: 7

Leone Bacciu
Leone Bacciu

Reputation: 46

You need to buy, or get for free, a DNS (you can get a subdomain from Noip for free). You then have to connect your domain to your router (search in the settings) and port forward your computer through the port you want. Now you will be able to visit "yourdomain.com" and you will get your django server. Don't forget to add your domain to ALLOWED_HOSTS. I wish it helps!

Upvotes: 0

Jason Yang
Jason Yang

Reputation: 306

You need to change the file settings.py so that my-site.com is contained in the list of ALLOWED_HOSTS.

For security reasons Django needs to know what server names it servers, which is specified by ALLOWED_HOSTS. On Django's web site it states that:

A list of strings representing the host/domain names that this Django site can serve. This is a security measure to prevent HTTP Host header attacks, which are possible even under many seemingly-safe web server configurations.

Django Settings: ALLOWED_HOSTS

Upvotes: 0

Gilbish Kosma
Gilbish Kosma

Reputation: 887

You can use LocalTunnel ,it allows you to easily share a web service on your local development machine without messing with DNS and firewall settings.

Install Localtunnel globally (requires NodeJS) to make it accessible anywhere:

npm install -g localtunnel

Run django server using

python manage.py runserver

and use the command line interface to request a tunnel to your local server:

lt --port 8000

It will give you a temporary url to use in any place,You can use that url in other sysytems too,the url will be live until the server is running in your system. You can also use the temporary url in facebook setting for testing purpose.

Upvotes: 0

Related Questions