【问题标题】:No such table on django website deployment on HerokuHeroku 上的 django 网站部署上没有这样的表
【发布时间】:2021-02-20 02:58:17
【问题描述】:

我之前在 Heroku 上部署我的 django 应用程序时遇到了关于静态目录的问题。但是现在我已经能够在更改 settings.py 后部署我的网站,出现一个名为“没有这样的表”的错误。然后我去stackoverflow寻求解决方案但没有任何效果。我在这里添加我的settings.py文件,其中也有stackoverflow解决方案。我已经编写了一个if语句来使用stackoverflow帖子中提到的PostgreSQL,但这也不起作用。你可能会看到最后一行的if语句。我的requirements.txt都是正确的,所以没有问题。请帮助我。

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '^&ahv#0-lkixr5bfk&)=2%$m$y8kg%z3wt_q)-m(&#u#^xu+pu'

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

ALLOWED_HOSTS = ['django-blog-sayan.herokuapp.com', '127.0.0.1']


# Application definition

INSTALLED_APPS = [
    'blog.apps.BlogConfig',
    'users.apps.UsersConfig',
    'crispy_forms',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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 = 'django_project.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 = 'django_project.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.1/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.1/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.1/howto/static-files/

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_TMP = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

STATICFILES_DIRS = [

    os.path.join(BASE_DIR, 'static'),

]

os.makedirs(STATIC_TMP, exist_ok=True)
os.makedirs(STATIC_ROOT, exist_ok=True)

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

CRISPY_TEMPLATE_PACK = 'bootstrap4'

LOGIN_REDIRECT_URL = 'blog-home'
LOGIN_URL = 'login'

if 'DATABASE_URL' in os.environ:
    import dj_database_url
    DATABASES = {'default': dj_database_url.config()} 

【问题讨论】:

  • 你做迁移和迁移了吗?
  • 是的,我这样做并再次部署
  • 部署后进入 heroku cli 并进行迁移和迁移

标签: python django heroku heroku-postgres django-deployment


【解决方案1】:

在绘制 Django 时,我总是遵循本教程。

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Deployment

在 cmets 中,您告诉我您进行了 makemigrations,然后在部署之前进行了迁移。

这些事情必须在 heroku 端部署后完成,因为它们将在 heroku 数据库上创建表。

【讨论】:

    猜你喜欢
    • 2020-12-31
    • 2018-12-11
    • 2015-04-28
    • 2020-10-30
    • 2016-12-21
    • 2018-01-18
    • 1970-01-01
    • 2023-03-12
    • 2015-05-15
    相关资源
    最近更新 更多