【问题标题】:I have deployed my django project in python anywhere but its not working我已经在 python 中的任何地方部署了我的 django 项目,但它不起作用
【发布时间】:2015-04-30 04:42:33
【问题描述】:

我已经在Python Anywhere 中部署了我的网站,但它无法正常工作。我已经将它上传到任何地方的 python,也更改了 WSGI 和 settings.py 但它只是显示welcome page.

服务器上的setting.py文件

 # Django settings for website project.
    DEBUG = True
    TEMPLATE_DEBUG = DEBUG

    ADMINS = (
        # ('Your Name', 'your_email@example.com'),
    )

    MANAGERS = ADMINS
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'django_db',
            'USER': 'rahulsatal',
            'PASSWORD': 'rahul',
            'HOST': '',
            'PORT': '',
        }
    }
    '''
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
            'NAME': '',                      # Or path to database file if using sqlite3.
            'USER': '',                      # Not used with sqlite3.
            'PASSWORD': '',                  # Not used with sqlite3.
            'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
            'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
        }
    }
    '''
    # Hosts/domain names that are valid for this site; required if DEBUG is False
    # See https://docs.djangoproject.com/en/1.3/ref/settings/#allowed-hosts
    ALLOWED_HOSTS = []

    # Local time zone for this installation. Choices can be found here:
    # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
    # although not all choices may be available on all operating systems.
    # On Unix systems, a value of None will cause Django to use the same
    # timezone as the operating system.
    # If running in a Windows environment this must be set to the same as your
    # system time zone.
    TIME_ZONE = 'America/Chicago'

    # Language code for this installation. All choices can be found here:
    # http://www.i18nguy.com/unicode/language-identifiers.html
    LANGUAGE_CODE = 'en-us'

    SITE_ID = 1

    # If you set this to False, Django will make some optimizations so as not
    # to load the internationalization machinery.
    USE_I18N = True

    # If you set this to False, Django will not format dates, numbers and
    # calendars according to the current locale
    USE_L10N = True

    # Absolute filesystem path to the directory that will hold user-uploaded files.
    # Example: "/home/media/media.lawrence.com/media/"
    MEDIA_ROOT = '/home/rahulsatal/website/media'

    # URL that handles the media served from MEDIA_ROOT. Make sure to use a
    # trailing slash.
    # Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
    MEDIA_URL = '/media/'

    # Absolute path to the directory static files should be collected to.
    # Don't put anything in this directory yourself; store your static files
    # in apps' "static/" subdirectories and in STATICFILES_DIRS.
    # Example: "/home/media/media.lawrence.com/static/"
    STATIC_ROOT = '/home/rahulsatal/website/static'

    # URL prefix for static files.
    # Example: "http://media.lawrence.com/static/"
    STATIC_URL = '/static/'

    # URL prefix for admin static files -- CSS, JavaScript and images.
    # Make sure to use a trailing slash.
    # Examples: "http://foo.com/static/admin/", "/static/admin/".
    ADMIN_MEDIA_PREFIX = '/static/admin/'

    # Additional locations of static files
    STATICFILES_DIRS = (
        # Put strings here, like "/home/html/static" or "C:/www/django/static".
        # Always use forward slashes, even on Windows.
        # Don't forget to use absolute paths, not relative paths.
    )

    # List of finder classes that know how to find static files in
    # various locations.
    STATICFILES_FINDERS = (
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    #    'django.contrib.staticfiles.finders.DefaultStorageFinder',
    )

    # Make this unique, and don't share it with anybody.
    SECRET_KEY = '-k$4lu9+wnz&nbnij!c8+f*ltoebjpn97^xu%ofra+)&5aa7vm'

    # List of callables that know how to import templates from various sources.
    TEMPLATE_LOADERS = (
        'django.template.loaders.filesystem.Loader',
        'django.template.loaders.app_directories.Loader',
    #     'django.template.loaders.eggs.Loader',
    )

    MIDDLEWARE_CLASSES = (
        'django.middleware.common.CommonMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
    )

    ROOT_URLCONF = 'website.urls'

    TEMPLATE_DIRS = (
        # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
        # Always use forward slashes, even on Windows.
        # Don't forget to use absolute paths, not relative paths.
    )

    INSTALLED_APPS = (
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        # Uncomment the next line to enable the admin:
        # 'django.contrib.admin',
        # Uncomment the next line to enable admin documentation:
        # 'django.contrib.admindocs',
        'website',
    )

    # A sample logging configuration. The only tangible logging
    # performed by this configuration is to send an email to
    # the site admins on every HTTP 500 error.
    # See http://docs.djangoproject.com/en/dev/topics/logging for
    # more details on how to customize your logging configuration.
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'handlers': {
            'mail_admins': {
                'level': 'ERROR',
                'class': 'django.utils.log.AdminEmailHandler'
            }
        },
        'loggers': {
            'django.request': {
                'handlers': ['mail_admins'],
                'level': 'ERROR',
                'propagate': True,
            },
        }
    }

服务器上的 WSGI 文件

"""
WSGI config for website project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
"""
import sys
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "website.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
DJANGO_SETTINGS_MODULE = 'website.website.settings'


sys.path.append('/home/rahulsatal/website')
# ...
DJANGO_SETTINGS_MODULE = 'website.settings'

我的错误日志是-

2015-02-26 22:02:05,989 :IOError: write error
2015-02-26 22:02:05,990 :RuntimeError: generator ignored GeneratorExit
2015-02-27 10:44:57,526 :/usr/lib/python2.7/threading.py:1160: RuntimeWarning: tp_compare didn't return -1 or -2 for exception
2015-02-27 10:44:57,526 :  return _active[_get_ident()]
2015-02-27 10:44:57,527 :Traceback (most recent call last):
2015-02-27 10:44:57,527 :  File "/bin/user_wsgi_wrapper.py", line 130, in __call__
2015-02-27 10:44:57,527 :    self.error_log_file.logger.exception("Error running WSGI application")
2015-02-27 10:44:57,527 :  File "/usr/lib/python2.7/logging/__init__.py", line 1185, in exception
2015-02-27 10:44:57,541 :    self.error(msg, *args, **kwargs)
2015-02-27 10:44:57,541 :  File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
2015-02-27 10:44:57,541 :    self._log(ERROR, msg, args, **kwargs)
2015-02-27 10:44:57,542 :  File "/usr/lib/python2.7/logging/__init__.py", line 1270, in _log
2015-02-27 10:44:57,542 :    record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
2015-02-27 10:44:57,542 :  File "/usr/lib/python2.7/logging/__init__.py", line 1244, in makeRecord
2015-02-27 10:44:57,542 :    rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2015-02-27 10:44:57,542 :  File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
2015-02-27 10:44:57,542 :    self.threadName = threading.current_thread().name
2015-02-27 10:44:57,542 :  File "/usr/lib/python2.7/threading.py", line 1160, in currentThread
2015-02-27 10:44:57,543 :    return _active[_get_ident()]
2015-02-27 10:44:57,543 :  File "/bin/user_wsgi_wrapper.py", line 122, in __call__
2015-02-27 10:44:57,543 :    app_iterator = self.app(environ, start_response)
2015-02-27 10:44:57,543 :  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 250, in __call__
2015-02-27 10:44:57,554 :    self.load_middleware()
2015-02-27 10:44:57,554 :  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 39, in load_middleware
2015-02-27 10:44:57,562 :    for middleware_path in settings.MIDDLEWARE_CLASSES:
2015-02-27 10:44:57,562 :  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 276, in __getattr__
2015-02-27 10:44:57,564 :    self._setup()
2015-02-27 10:44:57,564 :  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
2015-02-27 10:44:57,616 :    self._wrapped = Settings(settings_module)
2015-02-27 10:44:57,616 :  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 90, in __init__
2015-02-27 10:44:57,616 :    mod = importlib.import_module(self.SETTINGS_MODULE)
2015-02-27 10:44:57,616 :  File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
2015-02-27 10:44:57,617 :    __import__(name)
2015-02-27 10:44:57,617 :  File "./website/settings.py", line 33
2015-02-27 10:44:57,617 :    ALLOWED_HOSTS = []
2015-02-27 10:44:57,617 :                ^
2015-02-27 10:44:57,617 :SyntaxError: invalid syntax
2015-02-27 10:44:59,352 :Traceback (most recent call last):
2015-02-27 10:44:59,352 :  File "/bin/user_wsgi_wrapper.py", line 130, in __call__
2015-02-27 10:44:59,352 :    self.error_log_file.logger.exception("Error running WSGI application")
2015-02-27 10:44:59,352 :  File "/usr/lib/python2.7/logging/__init__.py", line 1185, in exception
2015-02-27 10:44:59,352 :    self.error(msg, *args, **kwargs)
2015-02-27 10:44:59,352 :  File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
2015-02-27 10:44:59,352 :    self._log(ERROR, msg, args, **kwargs)
2015-02-27 10:44:59,353 :  File "/usr/lib/python2.7/logging/__init__.py", line 1270, in _log
2015-02-27 10:44:59,353 :    record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
2015-02-27 10:44:59,353 :  File "/usr/lib/python2.7/logging/__init__.py", line 1244, in makeRecord
2015-02-27 10:44:59,353 :    rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2015-02-27 10:44:59,353 :  File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
2015-02-27 10:44:59,353 :    self.threadName = threading.current_thread().name
2015-02-27 10:44:59,353 :  File "/usr/lib/python2.7/threading.py", line 1160, in currentThread
2015-02-27 10:44:59,354 :    return _active[_get_ident()]
2015-02-27 10:44:59,354 :  File "/bin/user_wsgi_wrapper.py", line 122, in __call__
2015-02-27 10:44:59,354 :    app_iterator = self.app(environ, start_response)
2015-02-27 10:44:59,354 :  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 250, in __call__
2015-02-27 10:44:59,354 :    self.load_middleware()
2015-02-27 10:44:59,354 :  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 39, in load_middleware
2015-02-27 10:44:59,354 :    for middleware_path in settings.MIDDLEWARE_CLASSES:
2015-02-27 10:44:59,354 :  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 276, in __getattr__
2015-02-27 10:44:59,355 :    self._setup()
2015-02-27 10:44:59,355 :  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
2015-02-27 10:44:59,355 :    self._wrapped = Settings(settings_module)
2015-02-27 10:44:59,355 :  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 90, in __init__
2015-02-27 10:44:59,355 :    mod = importlib.import_module(self.SETTINGS_MODULE)
2015-02-27 10:44:59,355 :  File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
2015-02-27 10:44:59,355 :    __import__(name)
2015-02-27 10:44:59,355 :  File "./website/settings.py", line 33
2015-02-27 10:44:59,355 :    ALLOWED_HOSTS = []
2015-02-27 10:44:59,355 :                ^
2015-02-27 10:44:59,355 :SyntaxError: invalid syntax

请任何人帮助我。

【问题讨论】:

    标签: django deployment pythonanywhere


    【解决方案1】:

    在您的settings.py 中设置以下选项

    ALLOWED_HOSTS=['*']
    

    【讨论】:

    • 您能解释一下为什么在这种情况下添加该配置值会有所帮助吗?
    【解决方案2】:

    1.尝试: ALLOWED_HOSTS = ['rahulsatal.pythonanywhere.com']

    1. 通过更新 WSGI 文件激活你的 virtualenv

    activate_this = '/home/harunbiswas/.virtualenvs/django18/bin/activate_this.py'
    with open(activate_this) as f:
        code = compile(f.read(), activate_this, 'exec')
        exec(code, dict(__file__=activate_this))

    【讨论】:

      【解决方案3】:

      我认为您发布的错误日志可能不适用,因为此类错误意味着您会看到“出现问题”页面。另外,我认为您的 WSGI 文件可能没问题(同样,因为您看到的是欢迎页面而不是错误页面)。您应该检查的是您正在运行您认为正在运行的代码。您的 WSGI 文件所指向的目录中的 url 和视图是否指向您希望在那里看到的那些?

      【讨论】:

        【解决方案4】:

        您需要将'localhost''*' 添加到允许的主机 - 否则任何人都无法访问您的页面。

        ALLOWED_HOSTS = ('localhost')
        

        希望这会有所帮助!

        【讨论】:

        • 但我想在 https 上部署它仍然无法正常工作
        • https 与允许的主机无关。如果您想使用 https,请考虑安装 SSL 证书。
        • 现在使用 * ---出了点问题 :-( 显示
        • 确保其内容与此完全相同; ALLOWED_HOSTS = ('*')
        • 同样,只需使用这个(取自我的 Django 环境)ALLOWED_HOSTS = ( 'localhost' )
        猜你喜欢
        • 2015-05-05
        • 2015-05-10
        • 2023-04-05
        • 2018-10-30
        • 2023-01-19
        • 2021-08-18
        • 2020-06-15
        • 2018-08-11
        • 1970-01-01
        相关资源
        最近更新 更多