How can I detect whether a view is being called in a test environment (e.g., from manage.py test)?
#pseudo_code
def my_view(request):
if not request.is_secure() and not TEST_ENVIRONMENT:
return HttpResponseForbidden()
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Put this in your settings.py:
This tests whether the second commandline argument (after
./manage.py) wastest. Then you can access this variable from other modules, like so:There are good reasons to do this: suppose you’re accessing some backend service, other than Django’s models and DB connections. Then you might need to know when to call the production service vs. the test service.