应用程序在本地计算机上完美运行,但在Pythonanywhere.com上运行

时间:2017-03-06 08:48:05

标签: python django pythonanywhere

我的应用程序在本地计算机上工作得很好,但在Pythonanywhere.com上却没有,我无法找到这个问题背后的问题。甚至无法将更改迁移到数据库。执行命令python manage.py runserver时出现此错误

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/ananth1010/Social-media-App-Using-Django/myvenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in exe
cute_from_command_line
    utility.execute()
  File "/home/ananth1010/Social-media-App-Using-Django/myvenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 341, in exe
cute
    django.setup()
  File "/home/ananth1010/Social-media-App-Using-Django/myvenv/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/ananth1010/Social-media-App-Using-Django/myvenv/lib/python3.5/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/home/ananth1010/Social-media-App-Using-Django/myvenv/lib/python3.5/site-packages/django/contrib/admin/apps.py", line 23, in ready
    self.module.autodiscover()
  File "/home/ananth1010/Social-media-App-Using-Django/myvenv/lib/python3.5/site-packages/django/contrib/admin/__init__.py", line 26, in autodi
scover
    autodiscover_modules('admin', register_to=site)
  File "/home/ananth1010/Social-media-App-Using-Django/myvenv/lib/python3.5/site-packages/django/utils/module_loading.py", line 50, in autodisc
over_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/home/ananth1010/Social-media-App-Using-Django/myvenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/ananth1010/Social-media-App-Using-Django/posts/admin.py", line 2, in <module>
    from models import Post,Comment,User_Detailed_Info
ImportError: No module named 'models'
(myvenv) 07:18 ~/Social-media-App-Using-Django (master)$ python manage.py makemigrations posts
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/ananth1010/Social-media-App-Using-Django/myvenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in exe
cute_from_command_line
    utility.execute()
  File "/home/ananth1010/Social-media-App-Using-Django/myvenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 341, in exe
cute
    django.setup()
  File "/home/ananth1010/Social-media-App-Using-Django/myvenv/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/ananth1010/Social-media-App-Using-Django/myvenv/lib/python3.5/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/home/ananth1010/Social-media-App-Using-Django/myvenv/lib/python3.5/site-packages/django/contrib/admin/apps.py", line 23, in ready
    self.module.autodiscover()
  File "/home/ananth1010/Social-media-App-Using-Django/myvenv/lib/python3.5/site-packages/django/contrib/admin/__init__.py", line 26, in autodi
scover
    autodiscover_modules('admin', register_to=site)
  File "/home/ananth1010/Social-media-App-Using-Django/myvenv/lib/python3.5/site-packages/django/utils/module_loading.py", line 50, in autodisc
over_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/home/ananth1010/Social-media-App-Using-Django/myvenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/ananth1010/Social-media-App-Using-Django/posts/admin.py", line 2, in <module>
    from models import Post,Comment,User_Detailed_Info
ImportError: No module named 'models'

这是我的settings.py

"""
Django settings for my_blog project.

Generated by 'django-admin startproject' using Django 1.10.5.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'o@lvfcf%a_-ts*4-8&a_=-rk!gq0o7-zup1--&3zssg0c91_k-'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'posts',
    '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 = 'my_blog.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 = 'my_blog.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.10/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/1.10/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'

MEDIA_ROOT=os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'


import dj_database_url
DATABASES['default'] = dj_database_url.config()

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['*']

DEBUG = False

try:
    from .local_settings import *
except ImportError:
    pass

我该如何解决这个问题? THANKYOU

2 个答案:

答案 0 :(得分:4)

你的问题在这一行

from models import Post,Comment,User_Detailed_Info

你在Pythonanywhere.com上使用python 3.5,这个版本的python默认使用绝对导入。替换为

from posts.models import Post,Comment,User_Detailed_Info

更多阅读。 https://www.python.org/dev/peps/pep-0328/

答案 1 :(得分:1)

您正在导入

 from models import Post,Comment,User_Detailed_Info

在您的设置文件中,我看到您的模块是您需要调用的帖子

 from posts.models import Post,Comment,User_Detailed_Info