为什么pythonanywhere会出现DisallowedHost

问题描述 投票:0回答:1

为什么 pythonanywhere 显示 DisallowedHost at :

无效的 HTTP_HOST 标头:“kimmy.pythonanywhere.com”。您可能需要 将“kimmy.pythonanywhere.com”添加到 ALLOWED_HOSTS。

我已经在

ALLOWED_HOSTS = ['kimmy.pythonanywhere.com']
中设置了
setting.py
但没有任何改变。

pythonanywhere
1个回答
0
投票

我想我有完全相同的问题:我试图在 PythonAnywhere 上部署我的 Django 博客应用程序,但我收到错误,未从 .env 文件中读取 ALLOWED_HOSTS 和 SECRET_KEY (我在设置的同一文件夹中创建了它) .py,在主项目文件夹中)。

这是我的设置.py:

from dotenv import load_dotenv
from pathlib import Path
import logging
import os
import sys
    
load_dotenv()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
    
    
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
    
SECRET_KEY = os.getenv("SECRET_KEY")
    
Logger = logging.getLogger(__name__)
Logger.error(Path.cwd())
    
IS_PRODUCTION = os.getenv('DJANGO_PRODUCTION', 'False') == 'True'
    
if IS_PRODUCTION:
    DEBUG = False
    ALLOWED_HOSTS = ['sirjude.pythonanywhere.com']
    
    # # Security settings for production
    # SECURE_HSTS_SECONDS = 31536000  # 1 year
    # SECURE_HSTS_INCLUDE_SUBDOMAINS = True
    # SECURE_HSTS_PRELOAD = True
    SECURE_SSL_REDIRECT = True
    SESSION_COOKIE_SECURE = True
    CSRF_COOKIE_SECURE = True
else:
    DEBUG = True
    ALLOWED_HOSTS = ['localhost', '127.0.0.1']
...

这是我的 .env 文件,位于 settings.py 所在的同一文件夹中:

SECRET_KEY=<my django secret key>
DJANGO_PRODUCTION=False
DB_ENGINE=django.db.backends.postgresql
DB_NAME=app
DB_USER=super
DB_PASSWORD=<my postgrespassword>
DB_HOST=SirJude-4090.postgres.pythonanywhere-services.com
DB_PORT=14090

...但我不断收到此错误:

2024-10-08 12:46:38,548: Invalid HTTP_HOST header: 'sirjude.pythonanywhere.com'. You may need to add 'sirjude.pythonanywhere.com' to ALLOWED_HOSTS.
Traceback (most recent call last):
  File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/utils/deprecation.py", line 133, in __call__
    response = self.process_request(request)
  File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/middleware/common.py", line 48, in process_request
    host = request.get_host()
  File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/http/request.py", line 151, in get_host
    raise DisallowedHost(msg)
django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: 'sirjude.pythonanywhere.com'. You may need to add 'sirjude.pythonanywhere.com' to ALLOWED_HOSTS.
2024-10-08 12:46:38,704: Error running WSGI application
2024-10-08 12:46:38,705: django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
2024-10-08 12:46:38,705:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/core/handlers/wsgi.py", line 124, in __call__
2024-10-08 12:46:38,705:     response = self.get_response(request)
2024-10-08 12:46:38,706: 
2024-10-08 12:46:38,706:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 140, in get_response
2024-10-08 12:46:38,706:     response = self._middleware_chain(request)
2024-10-08 12:46:38,706: 
2024-10-08 12:46:38,706:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 57, in inner
2024-10-08 12:46:38,706:     response = response_for_exception(request, exc)
2024-10-08 12:46:38,706: 
2024-10-08 12:46:38,706:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 140, in response_for_exception
2024-10-08 12:46:38,706:     response = handle_uncaught_exception(
2024-10-08 12:46:38,706: 
2024-10-08 12:46:38,707:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 181, in handle_uncaught_exception
2024-10-08 12:46:38,707:     return debug.technical_500_response(request, *exc_info)
2024-10-08 12:46:38,707: 
2024-10-08 12:46:38,707:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/views/debug.py", line 69, in technical_500_response
2024-10-08 12:46:38,707:     html = reporter.get_traceback_html()
2024-10-08 12:46:38,707: 
2024-10-08 12:46:38,707:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/views/debug.py", line 428, in get_traceback_html
2024-10-08 12:46:38,707:     c = Context(self.get_traceback_data(), use_l10n=False)
2024-10-08 12:46:38,707: 
2024-10-08 12:46:38,708:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/views/debug.py", line 397, in get_traceback_data
2024-10-08 12:46:38,708:     "settings": self.filter.get_safe_settings(),
2024-10-08 12:46:38,708: 
2024-10-08 12:46:38,708:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/views/debug.py", line 156, in get_safe_settings
2024-10-08 12:46:38,708:     settings_dict[k] = self.cleanse_setting(k, getattr(settings, k))
2024-10-08 12:46:38,708: 
2024-10-08 12:46:38,708:   File "/home/SirJude/Blog/.venv/lib/python3.10/site-packages/django/conf/__init__.py", line 98, in __getattr__
2024-10-08 12:46:38,708:     raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")

任何帮助,请!!!

© www.soinside.com 2019 - 2024. All rights reserved.