[Django]-How do I reference a Django settings variable in my models.py?

434👍

Try with this: from django.conf import settings then
settings.VARIABLE to access that variable.

VARIABLE should be in capital letter. It will not work otherwise.

131👍

from django.conf import settings

PRIVATE_DIR = getattr(settings, "PRIVATE_DIR", None)

Where it says None, you will put a default value incase the variable isn’t defined in settings.

4👍

There’s an example of importing the EMAIL_HOST_USER from the settings.py file and use it to send an email:

from django.conf import settings
from django.core.mail import send_mail

def post_share(request, post_id): 
   # ...
   send_mail(subject, message, settings.EMAIL_HOST_USER, [
                      settings.EMAIL_HOST_USER, 'ahmnouira@gmail.com'])

Leave a comment