Home>

I'm studying the Django framework.
The description of settings.py was different from the one referred to when posting with an image, and an error occurred.

First of all, the description about BASE_DIR, but in the reference book

BASE_DIR = os.path.dirname (os.path.dirname (os.path.abspath (__ file__)))
DATABASES = {
    'default': {
        'ENGINE':'django.db.backends.sqlite3',
        'NAME': os.path.join (BASE_DIR /'db.sqlite3'),
    }
}


Is described in my settings.py

BASE_DIR = Path (__file__) .resolve (). parent.parent
DATABASES = {
    'default': {
        'ENGINE':'django.db.backends.sqlite3',
        'NAME': BASE_DIR /'db.sqlite3',
    }
}


It was described as.
At the bottom of settings.py according to the reference book

BASE_DIR = Path (__file__) .resolve (). parent.parent
DATABASES = {
    'default': {
        'ENGINE':'django.db.backends.sqlite3',
        'NAME': BASE_DIR /'db.sqlite3',
    }
}
MEDIA_ROOT = os.path.join (BASE_DIR,'media')
MEDIA_URL ='/ media /'


When
MEDIA_ROOT = os.path.join (BASE_DIR,'media')
MEDIA_URL ='/ media /'

I added the two lines of, but I couldn't start sever from the command prompt because os couldn't be found.

So I thought I should do it myself

BASE_DIR = Path (__file__) .resolve (). parent.parent
DATABASES = {
    'default': {
        'ENGINE':'django.db.backends.sqlite3',
        'NAME': BASE_DIR /'db.sqlite3',
    }
}
MEDIA_ROOT = BASE_DIR,'media'
MEDIA_URL ='/ media /'


With this description, sever started successfully, but when I posted a new image, the following error occurred.
TypeError at/post_create
_getfullpathname: path should be string, bytes or os.PathLike, not tuple

Looking into this error, it seems that path should be treated as a string instead of a tuple.
I'm wondering if the framework description rules are wrong in the first place, so could you please tell me how to solve it?

models.py

class Post (models.Model):
    author = models.ForeignKey (User, on_delete = models.PROTECT, blank = False)
    title = models.CharField ('title', max_length = 50)
    content = models.TextField ('content', max_length = 1000)
    category = models.ForeignKey ('Category', on_delete = models.PROTECT)
    thumbnail = models.ImageField (upload_to ='images /', blank = True)
    created_at = models.DateTimeField (auto_now_add = True)
    updated_at = models.DateTimeField (auto_now = True)


settings.py

BASE_DIR = Path (__file__) .resolve (). parent.parent
DATABASES = {
    'default': {
        'ENGINE':'django.db.backends.sqlite3',
        'NAME': BASE_DIR /'db.sqlite3',
    }
}
MEDIA_ROOT = BASE_DIR,'media'
MEDIA_URL ='/ media /'


By the way, I did import os and rewrote the contents of BASE_DIR in settings.py according to the reference book and ran it.
'NAME': BASE_DIR /'db.sqlite3',
TypeError: unsupported operand type (s) for /:'str' and'str'
I got an error like this.

  • Answer # 1

    It seems that it was confused by the fact that import os was missing at the first execution and the change in the description method of BASE_DIR.

    BASE_DIR = Path (__file__) .resolve (). parent.parent
    DATABASES = {
        'default': {
            'ENGINE':'django.db.backends.sqlite3',
            'NAME': BASE_DIR /'db.sqlite3',
        }
    }
    MEDIA_ROOT = os.path.join (BASE_DIR,'media')
    MEDIA_URL ='/ media /'


    I succeeded in posting a new POST with an image with this description.
    The version of Django is 3.1.2.