我已经在任何地方用Python部署了我的Django项目,但它不起作用

whhtz7ly  于 2023-06-25  发布在  Go
关注(0)|答案(5)|浏览(95)

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

在服务器上设置.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

请任何人帮助我。

fd3cxomn

fd3cxomn1#

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

bwleehnv

bwleehnv2#

1.尝试:ALLOWED_HOSTS = ['rahulsatal.pythonanywhere.com']
1.通过更新WSGI文件激活虚拟环境

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))
7bsow1i6

7bsow1i63#

settings.py中设置以下选项

ALLOWED_HOSTS=['*']
5jdjgkvh

5jdjgkvh4#

部署Python/Django-免费托管
托管步骤
1.服务器退出-按Ctrl + C
2. pip freeze > requirements.txt//这将为我们的项目创建一个文件requirements.txt,其中显示了我们在项目中使用的所有已安装的Python库
3.在vs代码中,右键单击包含src和venv的根项目文件夹。点击'reveal in file explorer'或shift + alt + R或打开我们的根文件夹。
然后右键单击www.example.com中的项目文件夹'添加到projectname.rar '//压缩为rar文件。src.click 'add to projectname.rar' //compress to rar file.
这个文件将被上传到django项目
4.访问www.example.com pythonanywhere.com
关于我们
创建一个初学者帐户
转到 Jmeter 板-单击文件-上传文件-然后加载我们的'projectname.rar'文件
转到dashboard-单击console-新建控制台-单击bash-这将在浏览器中打开一个终端
在cmd中键入pwd将显示当前文件夹
键入ls以显示文件夹中的当前文件
输入unrar x projectname.rar//解压我们的项目文件
如果是zip文件,则键入unzip x www.example.com projectname.zip
ls-显示项目文件夹
cd项目名称/
ls-显示我们的文件,如www.example.com,requirements. txt,.. manage.py,requirements.txt,..
右边三行,点击help,djangodeploy,复制粘贴下面的代码
键入mkvirtualenv –python=/usr/bin/python3.8 venv//这将创建虚拟环境
5. pip install -r requirements.txt
右侧三行,右键单击Web-在新选项卡中打开-添加新Web应用程序单击下一步-单击选择框架下的手动配置
选择python版本-点击下一步//显示一个浏览器显示我们的网址
右击配置'ourwebname'-在新标签中打开//这将是我们的网站
单击文件-在导航栏中-打开包含www.example.com的文件夹-复制路径manage.py - copy the path
转到网站设置页面-将路径粘贴到代码下的源代码中
复制粘贴路径到venv也像前面的过程
然后你可以看到,wsgi在code//滚动查看code,源码,wsgi,。。。
单击wsgi配置文件的路径链接。然后删除django代码部分上下的所有代码。然后取消注解取消注解import os,import sys,path,....
6.用我们的projectname而不是mysite重写路径代码
重写projectname.settings
然后保存。点击上一步
在我们的主页,点击重新加载我们的网页链接
然后在新标签页中打开我们的网页。它将显示错误,如不允许的主机
因此,转到文件,打开www.example.com,更改代码允许的主机-网站名称没有https和最后一个斜杠。settings.py点击保存,返回点击导航栏下的网页,然后点击重新加载我们的网站 Just need 'websitename.com' // use single quotes. Click save, back Click web under navigation bar, then click reload our site
7.如果显示错误,如template doesn't exist,请转到www.example.com,然后将代码更改为,TEMPLATES =[{................................................................................................................................................................................................................. settings.py然后单击保存。然后点击网页导航栏下的重新加载。现在我们可以看到我们的项目网站如果css不工作,点击网页在导航栏,向下滚动,以查看静态文件的网址。 Then click reload under web-navigation bar. Now we can see our project website If the css is not working, click web in navigation bar, scroll down to see the static files url .to get static files url.
点击导航栏中的文件,复制粘贴静态文件夹的路径。在url下键入'/static/'//与设置文件中的相同。在目录下粘贴静态的url。
在www.example.com文件中,settings.py file,

STATIC_ROOT = BASE_DIR/’static’
STATIC_URL = ‘static/’

评论或删除

STATICFILES_DIRS = [
os.path.join………
]

在浏览器中打开命令提示符窗口,

python manage.py collectstatic

键入yes以允许重写
然后在网页导航栏下重新加载。然后在浏览器中打开我们的网站。它将用css加载我们的项目
现在你的django项目已经上线了!!!!!
Happy hosting:)

ttisahbt

ttisahbt5#

您需要将'localhost''*'添加到允许的主机-否则没有人能够访问您的页面。

ALLOWED_HOSTS = ('localhost')

希望这有帮助!

相关问题