前提・実現したいこと
Macのターミナルを使ってローカルにあるPythonのDjangoで作ったwebアプリファイルをリモートサーバに転送すると転送先のURLで下記のようなエラーがでてしまいます。
エラーメッセージ
TypeError at /アプリネーム/ argument 1 must be str, not PosixPath Request Method: GET Request URL: http://相手URL/アプリネーム/ Django Version: 2.2.7 Exception Type: TypeError Exception Value: argument 1 must be str, not PosixPath Exception Location: /usr/local/lib/python3.6/dist-packages/django/db/backends/sqlite3/base.py in get_new_connection, line 194 Python Executable: /usr/local/bin/uwsgi Python Version: 3.6.9 Python Path: ['.', '', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages'] Server time: Sun, 25 Apr 2021 07:02:50 +0000 Error during template rendering In template /var/www/django/ファイル名/アプリネーム/templates/アプリネーム2/index.html, error at line 1 argument 1 must be str, not PosixPath 1 {% if latest_question_list %} 2 <ul> 3 {% for question in latest_question_list %} 4 <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li> 5 {% endfor %} 6 </ul> 7 {% else %} 8 <p>No polls are available.</p> 9 {% endif %} 10
下記の部分の文法でエラーがでてしまいました。
アプリネーム/index.html
{% if latest_question_list %} ←この行がエラーでています。 <ul> {% for question in latest_question_list %} <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li> {% endfor %} </ul> {% else %} <p>No polls are available.</p> {% endif %}
自分のPCのローカルサーバではしっかりwebアプリケーションは動作しています。どのようにすればリモートサーバでも動作させることができるでしょうか?下記ようなURLに転送させたいです。
このURLにSafariとChromeを使ってアクセスしましたがどちらとも同じエラーでした。
いろいろ調べてみてDjangoのsettings.pyがいけないのかなと?と考えていますがこのエラーは治りません。
###追記
Djangoのバージョンをローカルとリモートのバージョンを統一してからランサーバーしてでたエラー。
$python3 manage.py runserver 0.0.0.0:8000 Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). Exception in thread django-main-thread: Traceback (most recent call last): File "/Users/myfile/.pyenv/versions/3.6.5/lib/python3.6/threading.py", line 916, in _bootstrap_inner self.run() File "/Users/myfile/.pyenv/versions/3.6.5/lib/python3.6/threading.py", line 864, in run self._target(*self._args, **self._kwargs) File "/Users/myfile/.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/utils/autoreload.py", line 54, in wrapper fn(*args, **kwargs) File "/Users/myfile/.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/db/migrations/loader.py", line 212, in build_graph self.applied_migrations = recorder.applied_migrations() File "/Users/myfile/.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 73, in applied_migrations if self.has_table(): File "/Users/myfile/.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 56, in has_table return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()) File "/Users/myfile/.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 194, in get_new_connection conn = Database.connect(**conn_params) TypeError: argument 1 must be str, not PosixPa
マイグレーションを実行した際のエラー
Traceback (most recent call last): File "/Users//.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv self.execute(*args, **cmd_options) File "/Users//.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute output = self.handle(*args, **options) File "/Users//.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped res = handle_func(*args, **kwargs) File "/Users//.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/db/backends/base/base.py", line 233, in _cursor self.ensure_connection() File "/Users//.pyenv/versions/3.6.5/lib/python3.6/site- TypeError: argument 1 must be str, not PosixPath During handling of the above exception, another exception occurred: Traceback (most recent call last): File "manage.py", line 22, in <module> main() File "manage.py", line 18, in main execute_from_command_line(sys.argv) File "/Users//.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line utility.execute() File "/Users//.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Users//.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/core/management/base.py", line 336, in run_from_argv connections.close_all() File "/Users//.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/db/utils.py", line 224, in close_all connection.close() File "/Users//.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 248, in close if not self.is_in_memory_db(): File "/Users//.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 367, in is_in_memory_db return self.creation.is_in_memory_db(self.settings_dict['NAME']) File "/Users//.pyenv/versions/3.6.5/lib/python3.6/site-packages/django/db/backends/sqlite3/creation.py", line 12, in is_in_memory_db return database_name == ':memory:' or 'mode=memory' in database_name TypeError: argument of type 'PosixPath' is not iterable
###試したこと
settings.pyの
ALLOWED_HOSTS = [""]→ALLOWED_HOSTS = ["*"]
ワイルドカードを用いましたが、結果は変わらずでした。
エラーメッセージでてから改めてローカルでrunserverし直しましたがエラーでませんでした。
python3 manage.py runserver 0.0.0.0:8000
マイグレーションもやり直して特にエラーでませんでした。
python3 manage.py makemigrations python3 manage.py migrate
ぜひ、アドバイスいただきたいです。よろしくお願いします。
該当のソースコード
from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent DEBUG = True ALLOWED_HOSTS = ["*"] # Application definition INSTALLED_APPS = [ 'polls.apps.PollsConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'アプリネーム.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], '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', ], }, }, ] WSGI_APPLICATION = 'アプリネーム.wsgi.application' # Database # https://docs.djangoproject.com/en/3.2/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } # Password validation # https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] # Internationalization # https://docs.djangoproject.com/en/3.2/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.2/howto/static-files/ STATIC_URL = '/static/' # Default primary key field type # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
補足情報(FW/ツールのバージョンなど)
Python3.6.9
Django2.2.7
MacOS
terminal
回答1件
あなたの回答
tips
プレビュー