Template Directory Path
Asked Answered
C

6

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?

Clegg answered 20/6, 2013 at 8:49 Comment(0)
C
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.

Clegg answered 26/6, 2013 at 17:9 Comment(0)
T
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/

Trill answered 19/8, 2021 at 19:17 Comment(0)
P
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
Priming answered 18/2, 2021 at 14:42 Comment(0)
S
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')],
    ...
},]
Staysail answered 7/3, 2021 at 15:3 Comment(0)
R
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 = '-----------------------------------------'
Reginareginald answered 16/2, 2023 at 5:54 Comment(0)
Z
0

Add import os on top of the file, save it and then run the program.

Zsazsa answered 26/4 at 7:0 Comment(2)
Does this add detail that's not in Enadun's or Shaik's answer?Asci
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Larval

© 2022 - 2024 — McMap. All rights reserved.