Error reportingLink to this heading
When you're running a public site you should always turn off the
DEBUG setting. That will make your server run much faster, and will
also prevent malicious users from seeing details of your application that can be
revealed by the error pages.
However, running with DEBUG set to False means you'll never see
errors generated by your site -- everyone will just see your public error pages.
You need to keep track of errors that occur in deployed sites, so Django can be
configured to create reports with details about those errors.
Email reportsLink to this heading
Server errorsLink to this heading
When DEBUG is False, Django will email the users listed in the
ADMINS setting whenever your code raises an unhandled exception and
results in an internal server error (HTTP status code 500). This gives the
administrators immediate notification of any errors. The ADMINS will
get a description of the error, a complete Python traceback, and details about
the HTTP request that caused the error.
By default, Django will send email from root@localhost. However, some mail
providers reject all email from this address. To use a different sender
address, modify the SERVER_EMAIL setting.
To activate this behavior, put the email addresses of the recipients in the
ADMINS setting.
404 errorsLink to this heading
Django can also be configured to email errors about broken links (404 "page not found" errors). Django sends emails about 404 errors when:
DEBUGisFalse;Your
MIDDLEWAREsetting includesdjango.middleware.common.BrokenLinkEmailsMiddleware.
If those conditions are met, Django will email the users listed in the
MANAGERS setting whenever your code raises a 404 and the request has
a referer. It doesn't bother to email for 404s that don't have a referer --
those are usually just people typing in broken URLs or broken Web bots. It also
ignores 404s when the referer is equal to the requested URL, since this
behavior is from broken Web bots too.
You can tell Django to stop reporting particular 404s by tweaking the
IGNORABLE_404_URLS setting. It should be a list of compiled
regular expression objects. For example:
import re
IGNORABLE_404_URLS = [
re.compile(r'\.(php|cgi)$'),
re.compile(r'^/phpmyadmin/'),
]
In this example, a 404 to any URL ending with .php or .cgi will not be
reported. Neither will any URL starting with /phpmyadmin/.
The following example shows how to exclude some conventional URLs that browsers and crawlers often request:
import re
IGNORABLE_404_URLS = [
re.compile(r'^/apple-touch-icon.*\.png$'),
re.compile(r'^/favicon\.ico$'),
re.compile(r'^/robots\.txt$'),
]
(Note that these are regular expressions, so we put a backslash in front of periods to escape them.)
If you'd like to customize the behavior of
django.middleware.common.BrokenLinkEmailsMiddleware further (for
example to ignore requests coming from web crawlers), you should subclass it
and override its methods.
Filtering error reportsLink to this heading
Filtering sensitive informationLink to this heading
Error reports are really helpful for debugging errors, so it is generally
useful to record as much relevant information about those errors as possible.
For example, by default Django records the full traceback for the
exception raised, each traceback frame’s local variables, and the
HttpRequest’s attributes.
However, sometimes certain types of information may be too sensitive and thus
may not be appropriate to be kept track of, for example a user's password or
credit card number. So in addition to filtering out settings that appear to be
sensitive as described in the DEBUG documentation, Django offers a
set of function decorators to help you control which information should be
filtered out of error reports in a production environment (that is, where
DEBUG is set to False): sensitive_variables() and
sensitive_post_parameters().
- sensitive_variables(*variables)Link to this definition
If a function (either a view or any regular callback) in your code uses local variables susceptible to contain sensitive information, you may prevent the values of those variables from being included in error reports using the
sensitive_variablesdecorator:from django.views.decorators.debug import sensitive_variables @sensitive_variables('user', 'pw', 'cc') def process_info(user): pw = user.pass_word cc = user.credit_card_number name = user.name ...In the above example, the values for the
user,pwandccvariables will be hidden and replaced with stars (**********) in the error reports, whereas the value of thenamevariable will be disclosed.To systematically hide all local variables of a function from error logs, do not provide any argument to the
sensitive_variablesdecorator:@sensitive_variables() def my_function(): ...
- sensitive_post_parameters(*parameters)Link to this definition
If one of your views receives an
HttpRequestobject withPOST parameterssusceptible to contain sensitive information, you may prevent the values of those parameters from being included in the error reports using thesensitive_post_parametersdecorator:from django.views.decorators.debug import sensitive_post_parameters @sensitive_post_parameters('pass_word', 'credit_card_number') def record_user_profile(request): UserProfile.create( user=request.user, password=request.POST['pass_word'], credit_card=request.POST['credit_card_number'], name=request.POST['name'], ) ...In the above example, the values for the
pass_wordandcredit_card_numberPOST parameters will be hidden and replaced with stars (**********) in the request's representation inside the error reports, whereas the value of thenameparameter will be disclosed.To systematically hide all POST parameters of a request in error reports, do not provide any argument to the
sensitive_post_parametersdecorator:@sensitive_post_parameters() def my_view(request): ...All POST parameters are systematically filtered out of error reports for certain
django.contrib.auth.viewsviews (login,password_reset_confirm,password_change, andadd_viewanduser_change_passwordin theauthadmin) to prevent the leaking of sensitive information such as user passwords.
Custom error reportsLink to this heading
All sensitive_variables() and sensitive_post_parameters() do is,
respectively, annotate the decorated function with the names of sensitive
variables and annotate the HttpRequest object with the names of sensitive
POST parameters, so that this sensitive information can later be filtered out
of reports when an error occurs. The actual filtering is done by Django's
default error reporter filter:
django.views.debug.SafeExceptionReporterFilter. This filter uses the
decorators' annotations to replace the corresponding values with stars
(**********) when the error reports are produced. If you wish to override or
customize this default behavior for your entire site, you need to define your
own filter class and tell Django to use it via the
DEFAULT_EXCEPTION_REPORTER_FILTER setting:
DEFAULT_EXCEPTION_REPORTER_FILTER = 'path.to.your.CustomExceptionReporterFilter'
You may also control in a more granular way which filter to use within any
given view by setting the HttpRequest’s exception_reporter_filter
attribute:
def my_view(request):
if request.user.is_authenticated:
request.exception_reporter_filter = CustomExceptionReporterFilter()
...
Your custom filter class needs to inherit from
django.views.debug.SafeExceptionReporterFilter and may override the
following methods:
- class SafeExceptionReporterFilterLink to this definition
- SafeExceptionReporterFilter.is_active(request)Link to this definition
Returns
Trueto activate the filtering operated in the other methods. By default the filter is active ifDEBUGisFalse.
- SafeExceptionReporterFilter.get_post_parameters(request)Link to this definition
Returns the filtered dictionary of POST parameters. By default it replaces the values of sensitive parameters with stars (**********).
- SafeExceptionReporterFilter.get_traceback_frame_variables(request, tb_frame)Link to this definition
Returns the filtered dictionary of local variables for the given traceback frame. By default it replaces the values of sensitive variables with stars (**********).