6

I'm new to python. I'm using python 2.7.1 with django 1.5.1.

When I put this code:

TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]

in my settings.py, the terminal shows the following error:

File "/home/pipo/Desktop/mysite/mysite/settings.py", line 116, in <module>
    [os.path.join(BASE_DIR, 'templates')]
NameError: name 'os' is not defined

Can someone tell me the reason for this error?

zcoop98
  • 2,590
  • 1
  • 18
  • 31
enadun
  • 3,107
  • 3
  • 31
  • 34

5 Answers5

12

To fix this error:

File "/home/myUser/path/to/project/projectName/projectName/settings.py", line 116, in <module>
  os.path.join(BASE_DIR, 'templates')
NameError: name 'os' is not defined

I had to add this line at the beginning of settings.py:

import os

Then I get this error:

File "/home/myUser/path/to/project/projectName/projectName/settings.py", line 116, in <module>
  os.path.join(BASE_DIR, 'templates')
NameError: name 'BASE_DIR' is not defined

To fix this, I added this line to settings.py:

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

This will return the current file path. You might need to change the os.path.join(BASE_DIR, 'templates') part accordingly.

zcoop98
  • 2,590
  • 1
  • 18
  • 31
enadun
  • 3,107
  • 3
  • 31
  • 34
1

Simply change

DIRS = [os.path.join(BASE_DIR, 'templates')]

in settings.py into

[BASE_DIR/'templates']

Reference: https://docs.djangoproject.com/en/3.2/howto/overriding-templates/

Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
0

To fix the error:

File "C:\Users\shahi\telusko_projects\telusko\telusko\settings.py", line 57, in <module>
  'DIRS': [os.path.join(BASE_DIR,'templates')],
NameError: name 'os' is not defined

Just add this line in the top of settings.py:

from pathlib import Path,os
zcoop98
  • 2,590
  • 1
  • 18
  • 31
0

This how I make it work:

import os
...
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
...
TEMPLATES = [
{
    ...
    'DIRS': [os.path.join(BASE_DIR, 'core/templates')],
    ...
},]
gildniy
  • 3,528
  • 1
  • 33
  • 23
0

You simply need to import os:

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '-----------------------------------------'
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83