How to change 'localhost' url django - 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

You can use localhost instead of 127.0.0.1 as one of the workaround.
Use this script:
$ python manage.py runserver localhost:8000

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
I am not sure about the mac and linux one but for windows it worked, hopefully it will work for others too.

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.

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

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!

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:

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!

Related

Django + GUnicorn ASGI with SCRIPT_NAME

I have a Django application running with a gunicorn ASGI server and an NGINX reverse proxy for serving static content. All are packaged within a docker container.
Now I want to serve this container behind a reverse proxy with a path prefix, e.g. "mydomain.com/djangoapp/". The problem is, that Django doesn't know it's hosted under a subpath, and for example, the Django admin application then always redirects to the root path "/" instead of "/djangoapp/".
I already read that there are several settings that handle this problem.
I tried setting the "FORCE_SCRIPT_NAME" in the Django settings directly to "/djangoapp". It worked for the admin login page, but after clicking the login button it redirected to the wrong root "/".
I tried setting the "SCRIPT_NAME" environment variable of the gunicorn server to "/djangoapp". It did not apply at all.
I'm running now out of ideas on what else to try. Does anybody else have a solution for this problem?
FORCE_SCRIPT_NAME should work. For the post-login redirect you need to properly set LOGIN_REDIRECT_URL

Django Cookiecutter

I am trying to use Cookiecutter on a Digital Ocean server. (Not using Docker)
I followed the direction to install on Ubuntu 16 with Django, Postgres and Gunicorn. https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
I can not get past the allowed host error.
DisallowedHost at /
Invalid HTTP_HOST header: '128.199.100.100:8000'.
You may need to add '128.199.100.100' to ALLOWED_HOSTS
I have the setting in production.py
ALLOWED_HOSTS = env.list ( 'DJANGO_ALLOWED_HOSTS',
default = [ '128.199.100.100' ] )
Do I need to change any setting to make it a production environment?
The only documentation on the Cookiecutter site is for pythonAnywere and Docker. http://cookiecutter-django.readthedocs.io/en/latest/deployment-on-pythonanywhere.html
I just want a simple DO install. Can not find any documentation?
Thank you.
At this stage, you need to add '128.199.100.100:8000' (including the port) to your ALLOWED_HOSTS. You could set it as an environment variable in the runserver command:
ALLOWED_HOSTS = env.list ( 'DJANGO_ALLOWED_HOSTS',
default = [ '128.199.100.100', '128.199.100.100:8000' ] )
Or you could temporarily add it to the default in your settings.
DJANGO_ALLOWED_HOSTS=128.199.100.100:8000 ~/myproject/manage.py runserver 0.0.0.0:8000
Eventually, the tutorial will change gunicorn to use a socket file and you will access the website on port 80 with Nginx, so you won't need '128.199.100.100:8000' in your ALLOWED_HOSTS.
I had the similar problem and I believe I found a solution.
If you are using environmental variables that passes DJANGO_ALLOWED_HOST value (the most secure way of copying credentials when deploying).
Then look closely at the syntax where you are defining the list of IP addresses allowed for the host. The syntax of environmental variables is completely different from django environmental variables and that made me confused.
At first I defined DJANGO_ALLOWED_HOSTS using python syntax
export DJANGO_ALLOWED_HOSTS="['localhost', '127.0.0.1', '192.168.1.110']"
Which is completely incorrect as looking down the error message trace under Settings section, I got the following ALLOWED_HOSTS value
ALLOWED_HOSTS ["['localhost',", "' 127.0.0.1'"," ' 192.168.1.110']"
Which is just means the variables get parsed assuming completely different syntax. Making django settings into comma-separated string also didn't fix the issue
export DJANGO_ALLOWED_HOSTS="localhost, 127.0.0.1, 192.168.1.110"
The parsed result is the following, notice space character leading in the second and third element.
ALLOWED_HOSTS ['localhost', ' 127.0.0.1', ' 192.168.1.110']
It seems that the logic that parses address values is very particular and the only correct way of listing several hostsusing only comma as a separator.
export DJANGO_ALLOWED_HOSTS="localhost,127.0.0.1,192.168.1.110"

How to set multiple settings.py for sites framework django?

Am trying to set up multiple website with same base. While browsing, came to know Django has Sites framework which I could use.
I didnt get how to set multiple settings.py file with site id. Any ideas anyone?
Thanks in advance :)
To serve multiple sites from the same Django instance you do not need multilple settings.py files.
A simple method is to omit the SITE_ID setting from the Sites framework. Then include the Sites framework middleware:
'django.contrib.sites.middleware.CurrentSiteMiddleware'
This automatically passes a request object to Site.objects.get_current() on every request. It also allows your Django application to detect the current site via request.site.
You would need to make sure to setup multilple virtual hosts using your NGINX or apache instance to route traffic from each site to your server.
you can have multiple setting file for example develop.py and production.py
steps:
create a settings folder inside the project
Add all of the settings file to that folder
while running server
./manage.py runserver -- settings=project_name.settings.required_settingfile
for example:
./manage.py runserver --settings=myproject.settings.develop

Static (admin) files do not seem to be served correctly

I have problems with Django serving static files on the admin panel.
Calling http://vbox.me/admin/ (where vbox.me is aliased to the VM's IP) results in a blank page without stylesheets.
Calling http://vbox.me/static/admin/css/base.css though, which is part of the stylesheets that should be loaded when opening http://vbox.me/admin/, brings up the correct file.
Here is some essential information:
I recently started experimenting with Django.
My current version of Django is 1.8.
I'm running nginx (on Arch Linux within a VirtualBox on Windows 7) that passes every non-static file to uwsgi
uwsgi and nginx run as service using systemd
Running the Django development server standalone does not result in the given weird behavior.
Nginx configuration file:
# ...
http {
sendfile on;
upstream django {
server unix:/tmp/uwsgi.sock;
}
server {
# ...
location /static {
# static directory of the django project
alias /home/martin/projects/django_test/static;
}
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params;
}
}
}
INI file for uwsgi:
[uwsgi]
chdir=/home/martin/projects/django_test
wsgi-file=django_test/wsgi.py
chmod-socket=666
socket=/tmp/uwsgi.sock
master=true
vacuum=true
Important parts from settings.py within the Django project:
STATIC_ROOT = '/home/martin/projects/django_test/static/'
# using vbox.me to make sure the browser does not make up some strange url.
STATIC_URL = 'http://vbox.me/static/'
I'm pretty much "left in the dark", I can only guess that I made some mistake in configuring uwsgi or Django, since nginx seems to server the static files perfectly (at least when explicitly requesting them...)
Thanks in advance!
Edit: I will do a fresh setup of all the tools I use to deploy my app to make sure I made no mistakes. Additionally, I will be using virtualenv this time (which I haven't used previously). I'll let you know if that works!
After doing a fresh install of everything following this tutorial, everything works just fine now. Looks like I made some mistakes in configuration.
I also used Django 1.7 here, so Django 1.8 might have been the cause to my problems as well (though thats really unlikely)

Django tutorial on remote server: how to view in my browser?

I'm getting started with a Django tutorial, and I've run into a snag. Having created the sample "mysite" on my usual domain, I want to be able to display it in my browser. The tutorial points me to http://127.0.0.1:8000. However, that's not going to work, as I'm doing this remotely.
[background information]
What I have done, apparently successfully, is
django-admin.py startproject mysite
(created mysite directory containing four files)
python manage.py runserver
(Validating models... 0 errors found, etc.)
The absolute path is
/home/toewsweb/public_html/pythonlab/mysite
What URL should I be able to use to bring this up in my browser?
I also put mysite at
/home/toewsweb/mysite (since it's not supposed to go in a publicly accessible directory)
What URL should I be able to use in this case?
This is a virtual private server, so I have access to httpd.conf. I have downloaded and installed mod_wsgi and have added it to the Apache configuration. I actually did set a subdomain with a DocumentRoot of /home/toewsweb/public_html/pythonlab/mysite; however, when I point the browser to that subdomain, I just get the directory listing.
[/background information]
Right now, I just want to know how to view what I'm working on in my browser.
Thanks!
For development purposes, there's no need to mess about with configuring WSGI (although it's useful to know, as you will need to do it for production). Just start the dev server so that it listens to an external port:
./manage.py runserver 0:8000
This binds to the external IP address, so now you can access your Django site via port 8000 on that server:
http://whatever.my.ip.is:8000
You need to setup the apache WSGIScriptAlias directive in your VirtualHost to properly load python and your site. Django's docs have a great explanation on what you need to do.
Basic configuration
Once you’ve got mod_wsgi installed and activated, edit your httpd.conf file and add:
WSGIScriptAlias / /path/to/mysite/apache/django.wsgi
The first bit above is the url you want to be serving your application at (/ indicates the root url), and the second is the location of a "WSGI file" -- see below -- on your system, usually inside of your project. This tells Apache to serve any request below the given URL using the WSGI application defined by that file.
Next we'll need to actually create this WSGI application, so create the file mentioned in the second part of WSGIScriptAlias and add:
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
If your project is not on your PYTHONPATH by default you can add:
path = '/path/to/mysite'
if path not in sys.path:
sys.path.append(path)
just below the import sys line to place your project on the path. Remember to replace 'mysite.settings' with your correct settings file, and '/path/to/mysite' with your own project's location.
OR
The other option is to run the dev server so it's accesible externally like so:
python manage.py runserver 0.0.0.0:80
though please DO NOT use this in production. The dev server is single-threaded, and has not been auditing for security.