3

I have a single Django app that handles two (or more) parts of site, for example an "admin" and an "api" sections of the site. I also have normal html pages for the rest of the site, where no Django is needed.

I want to have the static html site on the root of my site, and the Django app on some sub-locations, for example

www.example.com        -> Apache static html site
www.example.com/admin/ -> Django app, serving the pages for the admin
www.example.com/api/   -> Django app, serving the pages for the api

At first I try to do so all form Django, using the urls like so:

# my_app/urls.py
...
url(r'^admin/', include('admin.urls')),
url(r'^api/', include('api.urls')),
url(r'^(?P<path>.*)$', ??????????),
...

But I couldn't figure out a good way to delegate the serving of those static pages to Apache. (It did work if I use url(r'^(?P<path>.*)$, 'django.views.static.serve', {'document_root': '/path/to/static/site'}), but the Django documentation forbids to use that on a production server.)


Then I tried with the apache configuration:

# httpd.conf
...
DocumentRoot /path/to/static/site
WSGIScriptAlias /admin /path/to/django/my_app/wsgi.py
WSGIScriptAlias /api   /path/to/django/my_app/wsgi.py
...

This worked as far as requests to root returned the static site and requests to both "admin" and "api" would return the Django site, but within Django I found no way to distinguish if the request came from '/admin' or from '/api'. (Plus I'm not sure if having two WSGIScriptAlias pointing at the same wsgi might cause some problems.)


If someone knows of a way to achieve this without having to split my Django app into two (or more) parts it would be greatly appreciated.

Yamanqui
  • 199
  • 3
  • 10

1 Answers1

4

I had the almost exact same problem. In Apaches virtual host config for the site use

WSGIScriptAliasMatch ^(/(admin|api)) /path/to/django/my_app/wsgi.py$1

instead of

WSGIScriptAlias /admin /path/to/django/my_app/wsgi.py
WSGIScriptAlias /api   /path/to/django/my_app/wsgi.py

Check these Q&A for further information:

Django (wsgi) and Wordpress coexisting in Apache virtualhost

Reconfiguring Apache to serve website root from new php source and specific sub-urls from old django site

Community
  • 1
  • 1
Creech
  • 258
  • 2
  • 12