0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Djangoで argument 1 must be str, not PosixPath エラーが発生する

0
Posted at

この記事は過去のエラー解決メモを整理したものです。
現在の推奨手順とは異なる可能性があります。
公式ドキュメントを確認して最新情報と差分がないかを確認してください。

事象

ローカル環境で作成したDjangoアプリをリモート環境へアップロードしたところ、HTML表示時に次のエラーが発生した。

argument 1 must be str, not PosixPath

環境

  • リモート環境
    • Django 2.2.7
    • Python 3.6.9
  • ローカル環境
    • Django 3.2
    • Python 3.10.2
  • Windows 11
  • VSCode 1.64.2

原因

  • ローカルとリモートのDjangoのバージョンが違った

対策

  1. ローカルのDjangoのバージョンを2.2に下げる
  2. ローカルのPythonをバージョンダウン
    Django 2.2 は Python 3.10.2 に対応していないため、今回は Python 3.9.7 に下げた。
  3. settings.py を下記のように変更する
# 3.2
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
# Django2.2.7
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__)))

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': str(BASE_DIR + 'db.sqlite3'),
# 'NAME': str(BASE_DIR / 'db.sqlite3'), だとエラーになる
    }
}
  1. ローカル環境へコピーしてマイグレーションを実行する

参考情報

ローカルサーバにあるPythonのDjangoで作ったwebアプリをリモートサーバの転送先のURLで表示させることができない。

TypeError at ✖︎✖︎ : argument 1 must be str, not PosixPath 対処法 | ウツボウTECH

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?