1

I would like to have multiple django projects living at the same root url like this:

example.com/ # controlled by home django project  
example.com/project-2 # controlled by a separate django project  
example.com/project-3 # controlled by yet another django project  

I am already redefining the LOGIN_REDIRECT_URL, etc. as suggested by this excellent answer, but I have discovered another hurdle. I am actually using the same apps in the projects that live at example.com/project-2 and example.com/project-3, which causes some non-trivial problems for linking to content inside of a django project that have thus far been solved with seemingly hacky solutions.

For example, you can never refer to '/' in any template in either example.com/project-2 or example.com/project-3 to return to the root of the django project hosted at either of these URLs --- this will link to the home django project at example.com. To get around this, I have made a context processor that correctly prepends the root url of the project based on a custom settings.py variable SCRIPT_NAME: '' (for example.com), '/project-2' (for example.com/project-2), or '/project-3' (for example.com/project-3). This is all fine and good except that you need to do the same thing in the get_absolute_url functions. Before I knew it, I had just turned a bunch of code that was very reusable (by people other than myself) into code that was not reusable at all.

Is there a way to accomplish the same effect without having to prepend absolute URLs with the SCRIPT_NAME? Perhaps something clever with apache or mod_wsgi configuration? I am at a loss and hoping someone can help...

EDIT:

My apache configuration for example.com looks like this:

# redirect un-'/'-terminated urls to the '/'-terminated root urls
RewriteEngine On
RewriteRule /project-2$ /project-2/ [R=302,L]
RewriteRule /project-3$ /project-3/ [R=302,L]

# mod wsgi setup
WSGIScriptAlias /project-2 /srv/project2/project-2.wsgi
WSGIScriptAlias /project-3 /srv/project3/project-3.wsgi
WSGIScriptAlias / /srv/project1/project-1.wsgi
Community
  • 1
  • 1
dino
  • 3,093
  • 4
  • 31
  • 50

1 Answers1

3

You don't show how you're serving these projects from your Apache configuration, which would have been useful. But if you define them as separate WSGIScriptAlias directives, then SCRIPT_NAME is automatically passed through for you, and Django takes it into account when reversing and creating URLs.

WSGIScriptAlias /project-2 /srv/project2/project2.wsgi
WSGIScriptAlias /project-3 /srv/project3/project3.wsgi
WSGIScriptAlias / /srv/project1/project1.wsgi
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • See edits above. My apache setup is much like you describe (+RewriteRules if that makes a difference). How does Django automatically take this into account when reversing and creating URLs? This did not work for me out of the box... – dino Jun 22 '11 at 11:11
  • correction, the get_absolute_url and using the url template tag DOES work out of the box. The problem is only with hard-coded URLs. Thanks for your help! – dino Jun 22 '11 at 11:45
  • Daniel is right it should just work. That said, you do need to use the appropriate template tag and other Django functions for URL construction. In templates you should use the 'url template tag. See 'https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#url'. Are you actually using that template tag or are you hardwiring URLs into templates when you shouldn't be? – Graham Dumpleton Jun 22 '11 at 11:47
  • I had a few miscellaneous URLs (unfortunately in the navigation bar) where I was not using the 'url' template tag. Bad form, I know... Thanks for chiming in on this too, Graham. – dino Jun 22 '11 at 15:01