Registration and email do not work for Django for 500

I cannot get the log to work in my Django web application.

My settings file is as follows:

EMAIL_HOST = "smtp.gmail.com" EMAIL_PORT = 465 EMAIL_HOST_USER = " paulhtremblay@gmail.com " EMAIL_HOST_PASSWORD = "password" EMAIL_USE_TLS = True DEFAULT_FROM_EMAIL = " paulhtremblay@gmail.com " SERVER_EMAIL = 'smtp.gmail.com' ADMINS = ( ('Paul Tremblay', ' paulhtremblay@gmail.com '), ) LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'file': { 'level': 'ERROR', 'class': 'logging.FileHandler', 'filename': '/var/log/django_logs/debug.log' }, 'mail_admins': { 'level': 'ERROR', 'class': 'django.utils.log.AdminEmailHandler' }, }, 'loggers': { 'django': { 'handlers': ['file'], 'level': 'ERROR', 'propagate': True, }, 'django.request': { 'handlers': ['file'], 'level': 'ERROR', 'propagate': True, }, }, } 

I have this in my views file:

  def five_hundred_test(request): raise ValueError("raising this on purpose for testing for 500") return render(request, 'my_test/basic_css.html') 

When I point my browser to this function, I get a 500 error (as expected), but nothing is sent to my email address and nothing is put into the log file. I am using Django 1.9, with python3, using Apache to start the server.

+4
source share
2 answers

This is my working setup. I did not add console logger as I use it only for production. The inspiration came from the relevant protocol protocols in Django docs.

Here are my email and registration settings:

 from os.path import join # Email Configuration ========================================================== ADMINS = [('Me', ' my@email '), ] MANAGERS = ADMINS DEFAULT_FROM_EMAIL = ' from@email ' SERVER_EMAIL = ' error_from@email ' EMAIL_HOST = 'smtp.server' EMAIL_HOST_USER = 'username_for_login' EMAIL_HOST_PASSWORD = 'password_for_login' EMAIL_PORT = 587 EMAIL_USE_TLS = True # Logging Configuration ======================================================== LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'verbose': { 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s' }, 'simple': { 'format': '%(levelname)s %(message)s' }, }, 'handlers': { 'file': { 'level': 'ERROR', 'class': 'logging.FileHandler', 'filename': join(HOME_DIR, 'my_logs', 'debug.log'), 'formatter': 'verbose' }, 'mail_admins': { 'level': 'ERROR', 'class': 'django.utils.log.AdminEmailHandler', 'formatter': 'simple' }, }, 'loggers': { 'django': { 'handlers': ['file'], 'level': 'ERROR', 'propagate': True, }, 'django.request': { 'handlers': ['mail_admins'], 'level': 'ERROR', 'propagate': True, }, }, } 

I found two errors for environment variables:

  • The SERVER_EMAIL parameter must be an email address.
  • You are using TLS , and therefore the correct port must be 587.

In addition, you are trying to use Google as an SMTP server, but according to this updated answer, this service is no longer available from Google, and you need to look for another SMTP server.

I also compiled the message using several debugging methods if you need to use.

+1
source

This can be made much simpler . If you want to enter both the email and Apache log files, you need to do the following:

  • Configure email settings - this ensures that Django can send emails

     SERVER_EMAIL = ' me@me.eu ' EMAIL_HOST = 'smtp.me.eu' EMAIL_PORT = 587 EMAIL_USE_TLS = True EMAIL_HOST_USER = SERVER_EMAIL EMAIL_HOST_PASSWORD = 'password' 
  • Set DEBUG to False - this allows you to send emails by email

     DEBUG = False 
  • Add email recipients to ADMINS (you might want MANAGERS ) - this means which receives error messages

     ADMINS = ( ('Me', ' me@me.eu '), ) MANAGERS = ADMINS 
  • Finally, remove the standard console filter that prevents messages from being sent through , which allows errors to end in the Apache error logs

     DEFAULT_LOGGING['handlers']['console']['filters'] = [] 
+1
source

All Articles