Django + GUnicorn ASGI with SCRIPT_NAME - django

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

Related

Traefik and Django on a Subpath

Following setup I want to achieve inside Docker with Traefik and Django:
http://domain/app1
http://domain/app2
My docker-compose.yml contains the following labels for the containers:
traefik.http.routers.app1.rule=Host(`my.host.de`) && PathPrefix(`/app1`)
traefik.http.routers.app1.middlewares=app1
traefik.http.middlewares.app1.headers.customresponseheaders.SCRIPT_NAME=/app1
I did the same for app2.
In the settings.py of both apps I set: FORCE_SCRIPT_NAME = env('FORCE_SCRIPT_NAME', default=None) which then should get resolved via the ENV File where I have FORCE_SCRIPT_NAME=/app1.
On Django side I always get a 404 with the message that this path does not exist and I should choose from an existing one.
Django recognizes the URL as http://my.host.de/app1 and tells me The current path, app1, didn't match any of these.
EDIT: Since my setup is thought to be both for dev and prod envs, I am using the Django built in server as well as Gunicorn for Running the Django apps.
If you want to pass a SCRIPT_NAME header to django, you have to use customrequestheaders instead of customresponseheaders when creating the middleware

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
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!

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

Django-CMS text editing not working on Apache

When serving my django-cms application on apache with mod_wsgi I can't get the text-plugin to work.
The difference is that when serving on apache the textedito tries to get an url that does not exist.
On Apache:
/admin/js/iframe/default/wymiframe.html
With manage.py runserver
/static/cms/wymeditor/iframe/default/wymiframe.html
the wymiframe.html file is present in my static folder and is accessible but not via the wrong URL that I get when running the app through apache.
What do I do to get the text-plugin requesting the right URL?
What have you set STATIC_URL to in your settings file? That in part is what controls paths when links are created.
https://docs.djangoproject.com/en/1.4/howto/deployment/wsgi/modwsgi/#serving-the-admin-files
My problem was that I served static content on a subdomain and I had problems with Access Control.
My solution was to set
Header set Access-Control-Allow-Origin *
in my apache config file

Django + Lighttpd

I am having some trouble getting django and lighttpd setup.
Here ist what I want to do:
Have lighttpd manage the fascgi processes (they both are run on the same server)
Have the django app run at the root of the site (so no django.fcgi)
Alternativly I could start django on startup, but I would need a start/stop script for ubuntu/debian.
Also how do I handle the media / admin media urls? Also I would like to serve /css /js /img from the document root ...
TIA
Patrick
Please see:
https://github.com/cdsi/elrond/blob/master/etc/lighttpd.conf
https://github.com/cdsi/elrond/blob/master/var/www/elrond.wsgi
Be sure to update the env.ELROND_XXX paths accordingly, and change:
fastcgi.server = ("/elrond" =>
to:
fastcgi.server = ("/" =>