Make django handle subdomain suffix - django

We're hosting several dockerized web-apps on our webserver, let's call it group.example.com. Our subdomains are handled via nginx as suffixes though, which translates to something like group.example.com/app1/ group.example.com/app2/ as root urls.
When using Django, we run into problems though, as all its urls generated by url in the templates such as home will be relative links, so rendered to home. This relative link will not be interpreted correctly, leading to the main, non-app page group.example.com.
So the goal is to have an app based prefix such as /app1/ for all links. I can hardcode these for static links, but there has to be a more elegant way. Also this leads to problem for the used forms submitting to the wrong page - redirecting again back to the main, non-app page group.example.com.
I tried adding /app1/ to all registered urls as prefix, but that doesn't seem to work either - that way the app is running but user would need to visit group.example.com/app1/app1/ to get to the index, and the relative links still don't work correctly.
In the app docker-container we're running the web-app with nginx and uwsgi. It works fine when using correct subdomains such as app1.example2.com - but we don't have that capability on our new faster webserver we want to host the app on.
Is there a way to resolve this using the app containers nginx, uwsgi or django / middleware config to get the links to resolve to group.example.com/app1/ as root?

As far as I know, there is two ways to resolve it.
One use SCRIPT_NAME in the NGINX configuration. For example, based on this server fault answer:
location /app1/ {
SCRIPT_NAME /app1;
# rest of the config
}
Two You can add FORCE_SCRIPT_NAME in your settings.py:
FORCE_SCRIPT_NAME = '/app1'
FYI, I would prefer using first solution.

Related

Django, website with changed prefix, how to fix urls globally

I recently deployed my app locally with the help of Apache. I use server name and sufix to get to the content as http://my-server/app. The Problem is every button ignores what the root of my app is. So from my main view at my-server/app I try to redirect to /second_page and it sends me to server/second_page instead of server/app/second_page. Do I have to change every form by hand or is there any way to configure it through settings.py or urls.py?

Issue with Django URLs on apache production server

I have a Django site and I have set up a test server for it. Now, the test server is being used for something else (php) also and I have configured it as aliases in virtual host config for Apache.
Now the problem is this:
When I open it as: http://x.x.x.x/djangoSite/
the WSGI triggers Django and site loads perfectly. But still all the anchor tags point to http://x.x.x.x/viewFunction/ and not http://x.x.x.x/djangoSite/viewFunction/ and hence I get a 404 every time...
Is there a way to tell Django that the base URL for the site is not the domain (public IP) but instead domain/djangoSite/ from where the WSGI alias starts...
Regards
You don't show the code, but you are probably hard-coding the URLs in your templates. Don't do this: you should always use the {% url %} tag to calculate them dynamically. When you do this, Django will automatically apply the base path (passed as the SCRIPT_NAME parameter in the WSGI environment) and all will be well.

Nginx and Django on Dotcloud

I currently have a dotcloud app that uses django to serve everything. It works great, however, we recently had our site redone in angular.js, and I don't want to use django to serve the actual html pages (I want to just use nginx for that), but I want django to serve some links for the API we built for the angular code to use.
Is it possible for me, in the same app, to configure nginx to serve some static files for particular urls, and have it send other urls for django to serve?
I want nginx to serve my index.html page is a request comes in to wwww.example.com, but if a request for example.com/api/login/ comes in, I want that to be handled by django. Is this possible?
Yes, you can do what you are looking for, you just need to add an nginx.conf to your project and then specify which urls you want nginx to serve and which ones you want django to serve, by default they will all go to django, so you just need to specify which ones you want to be served by nginx.
Here is a link to the documentation: http://docs.dotcloud.com/0.4/guides/nginx/
Link to the nginx documentation on location blocks: http://wiki.nginx.org/HttpCoreModule#location
Here is an example for serving static files from nginx, you can use this as a guide to do what you need.
location /media/ {
root /home/dotcloud/data ;
}
location /static/ {
root /home/dotcloud/volatile ;
}

Hosting Django Project at /test #login_required redirects to /accounts instead of /test/accounts

I'm moving a project to new hosting and would like to set it up such that it sits at mysite.com/test/ (this is under mod_wsgi on an Apache server). This seems to do alright for the application itself, but when I use #login_required to enforce authentication Django redirects to mysite.com/accounts/login instead of mysite.com/test/accounts/login as I would like. I also have a mysite.com/prod that I want to do this same thing on so I don't want to hard code this anywhere in settings... it should figure out where the root of its URL is and act accordingly.
How do I set it up so that Django automagically redirects to what Apache considers that application's web root?
You need to set LOGIN_URL and LOGOUT_URL to full URL path in Django settings file. See:
http://docs.djangoproject.com/en/1.3/ref/settings/#login-url
Django doesn't automatically insert the mount point at the start of those as so have to be fully qualified.
The same problem can be solved in a more generic way for all project URLs. You could checkout an alternative solution at Running a Django site on my local machine, am I redirecting my URLs properly? for an environment based ROOT URL support.

django url scheme in apache and dev server

I have a a django application which is being served from apache/mod_wsgi under www.mysite.com/mysite
suppose I redirect url "myapp" -> myapp/urls.py
so to visit it from apache I will visit www.mysite.com/mysite/myapp/page1
to visit it from dev server I will need to visit www.mysite.com/myapp/page1
it also means absolute URLs wil be different in both cases
so what is the best way to handle this , so that app works same way in apache and dev server?
Don't embed absolute and/or non-computed URLs in your code or database. They will always come back and bite you on the ass.
Use either an alternate settings.py, or have some logic in settings.py to tweak differences between development/staging/production. We use settings.py as the production file and dev/staging use a local_settings.py which is tested for in settings.py and, if present, overrides production settings in settings.py. This prevents alternate development settings from creeping into staging/production.
Set a BASE_URL for the entire site and use it for everything else.
We go a bit further and have STATIC_MEDIA_URL and BIG_CONTENT_URL (for MP3s and Flash video) as the base URLs for other stuff.
All of this allows us to use whatever server is right for the moment. When I'm doing development I normally let the MEDIA come from the production servers (it's faster), but sometimes I'm doing a reorg of the media directories and I can't do it on production without breaking the world. So I just change my local_settings.py file to use my copy of the directories.