Say I have two sites that I want to serve from the same deployment, but I’d like to have them use different middleware and URLs. For example, one site might be a conventional, server-side template driven site, and another is a REST API. Or another site might have nothing but the Django Admin enabled. Is there a good way to do this?
I realize that a straightforward way to get the same end result is to have separate settings files and deploy them as separate sites that share a lot of code. I’ve done that sort of thing in the past. But these sites share enough code and models where I would prefer to be able to deploy it as one thing and not have to worry about coordinating separate deployments.
I’ve also done things where gating happens upstream, e.g. having nginx configuration for two server URLs which reverse proxy to the same Django for their requests, but allowing requests to /admin to only happen from one of them. That sort of power is great, but I wanted to figure out if I could get a simpler version where I can get a similar effect but push just Django stuff to a PaaS without the nginx layer. I’d like to make it as simple to deploy as possible.
I think I could do the URLs half of this by making custom middleware that swaps out the urlconf
attribute on the requests at the top of the chain (as described in this great article by @valentinogagliardi). That’s a little more surprising-to-new-devs than I would like, but I could imagine leaving lots of comments in the settings and urls code and living with the tradeoff.
For the middleware, I’ve only been able to think of terrible, terrible, monkey-patching hacks that will undoubtedly cause more trouble than they’re worth in the long run. For instance, having something that wraps middleware methods and can no-op depending on the the requests urlconf
attribute.
I think in my ideal world, I’d be able to declare request/response pipelines based on some top level criteria (domain or top level dir of the URL), and have each one of them define its own URLs and middleware configuration. I have no idea if such a feature is even desirable for Django as a whole though, since this seems like very much an edge case for people.
Anyhow, I’m curious if anyone else has done this. Thank you.