django - Change project wide variables -
i have built web application using django that, among other things, can send emails clients. when i'm in development mode, don't want testing emails go clients. instead want re-route email me.
currently, when developing on dev server (which has clone of code via git) comment out lines have client's email address, , add email address. seems poor method of handling situation. i've been burned (sending out 1,700 emails clients and, oh look, they're all coming email address... brutal)
i thinking of implementing project wide variable called dev lives in settings.py. then, i'd build context processor returns variable's value. way, in views, call context processor and, if dev true, set email-to value email address. otherwise, email-to address client email. finally, set git ignore settings.py file dev server dev variable true , production server dev variable false.
problem solved? approach? better ways of tackling this?
edit
it seems i'm misunderstanding fact context processors used in template, not in view. suppose question needs be, how handle can check variable view?
this want apart context processor. has nothing views.
what following decorator:
def email(function=none): def _decorator(view_function): def _view(request, *args, **kwargs): if settings.dev: settings.email_to = 'mymail@host.com' return view_function(request, *args, **kwargs) _view.__name__ = view_function.__name__ _view.__dict__ = view_function.__dict__ _view.__doc__ = view_function.__doc__ return _view if function: return _decorator(function) return _decorator this way can
@email def my_view(request, args): #send emails here return httpresponse("mail sent") i think explicit decorators far better using middlewares (which confused contextprocessor).
hope helps!
Comments
Post a Comment