Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django For Beginers - Joinpath error when running manage.py runserver

I am learning Djnago and I am facing an issue I have looked around but can't maange to solve it. So basically on page 45 of Django For Beginners the author says :

" Next we need to update config/settings.py to tell Django the location of our new templates directory. This is a one-line change to the setting 'DIRS' under TEMPLATES." Code

config/settings.py

TEMPLATES = [
{
...
'DIRS': [str (BASE_DIR. joinpath('templates '))], # new
...
},
]

So in my settings.py file I have this :

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [str(BASE_DIR.joinpath('templates'))],  # new
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Unfortunatelly, when I run the manage.py runserver I then get this message :

  File "C:\Users\User\Desktop\pages\manage.py", line 22, in <module>
    main()
  File "C:\Users\User\Desktop\pages\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\User\.virtualenvs\pages-8ZErAoFj\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\User\.virtualenvs\pages-8ZErAoFj\lib\site-packages\django\core\management\__init__.py", line 363, in execute
    settings.INSTALLED_APPS
  File "C:\Users\User\.virtualenvs\pages-8ZErAoFj\lib\site-packages\django\conf\__init__.py", line 82, in __getattr__
    self._setup(name)
  File "C:\Users\User\.virtualenvs\pages-8ZErAoFj\lib\site-packages\django\conf\__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\User\.virtualenvs\pages-8ZErAoFj\lib\site-packages\django\conf\__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "c:\users\user\appdata\local\programs\python\python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "C:\Users\User\Desktop\pages\config\settings.py", line 58, in <module>
    'DIRS': [str(BASE_DIR.joinpath('templates'))],  # new
AttributeError: 'str' object has no attribute 'joinpath'

I bet the error is very basic and I am not looking in the right direction but it's been nearly a day since I have started searching for an answer so if someone could help me I would be really gratefull.

Thanks all and have a great day.

like image 267
lag37 Avatar asked Feb 14 '26 23:02

lag37


1 Answers

First, you need to "import os" on settings.py.

import os

Then go to TEMPLATES and change the ...

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

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

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates'),],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},

]

#I hope you get your solution. Thank You.

like image 121
Sk Kamal Hossain Avatar answered Feb 16 '26 16:02

Sk Kamal Hossain