Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to alter django settings based on current request?

Tags:

django

I'm running multiple sites from a single django instance, and expect the framework to eventually serve several hundred sites from one or several installations.

I need to patch some django settings based on the current request. I've written some middleware to monkey patch the settings, but I need these settings to be patched before the middleware gets invoked because other apps aren't taking the monkey-patched changes (i.e. apps get run then middleware gets run so the apps don't use the monkey-patched settings).

I should also add this is mainly for the benefit of third-party apps that I haven't written, so I don't want to go round adding decorators or anything like that because that would mess up my upgrade path.

So:

  • How can I get access to the current request in an app's init.py file?
  • Will an app's init.py get called once per request or only once? If it's only once, how else could I do this so I can manipulate the settings once per request?
  • Is it safe to do this kind of monkey patching? I know it makes code a bit more opaque, but I don't want to use different wsgi files per site because I want to allow users to edit some of these settings and have my monkey patching come from the database.
  • Is there a better solution that would allow certain settings to be stored in the database?
like image 680
Paul J Avatar asked Oct 16 '25 23:10

Paul J


1 Answers

Never ever ever change settings on the fly. You cannot predict how the application may one day be deployed, and in most of them the project will fail in.. interesting ways.

like image 123
Ignacio Vazquez-Abrams Avatar answered Oct 18 '25 15:10

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!