【问题标题】:Operational Error No such table Django操作错误没有这样的表Django
【发布时间】:2015-07-31 17:47:30
【问题描述】:

您好,我正在学习 djangogirls 教程,但遇到了 OperationalError no such table:blog_post。这在我的虚拟环境中非常有效,但是在推送到 heroku 后我得到了 OperationalError。

我正在使用 Django 1.7.7。有关该错误的更多信息,请访问:https://girlsblog.herokuapp.com/

我已经完成了 python manage.py makemigrations python manage.py migrate 最初在教程的开头。然后我尝试再次执行此操作,但出现“未检测到更改”和“无需迁移”

这是我的 post_list.html

{% extends 'blog/base.html' %}

{% block content %}
    {% for post in posts %}
        <div class="post">
            <div class="date">
                {{ post.published.date }}
            </div>
            <h1><a href="{% url 'blog.views.post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
            <p> {{ post.text|linebreaks }}</p>
        </div>
    {% endfor %}
{% endblock content %}

这是我的 .gitignore

myvenv
__pycache__
staticfiles
local_settings.py
db.sqlite3

模型.py

from django.db import models
from django.utils import timezone

# Create your models here.

class Post(models.Model):
    author = models.ForeignKey('auth.User')
    title = models.CharField(max_length=200)
    text = models.TextField()
    created_date = models.DateTimeField(
            default=timezone.now)
    published_date = models.DateTimeField(
            blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

Settings.py

"""
Django settings for mysite project.

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

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

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


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '1l8)#&q8r_wwev1r9mm8q5ezz8p#)rvg(l4%(t^-t8s4bva2+r'

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
)

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

ROOT_URLCONF = 'mysite.urls'

WSGI_APPLICATION = 'mysite.wsgi.application'


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

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

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Chicago'

USE_I18N = True

USE_L10N = True

USE_TZ = False


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

STATIC_URL = '/static/'

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

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['*']

STATIC_ROOT = 'staticfiles'

DEBUG = False

try:
    from .local_settings import *
except ImportError:
    pass

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

blog/views.py

from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from .models import Post
from .forms import PostForm

# Create your views here.

def post_list(request):
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'blog/post_list.html', {'posts': posts})

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})

def post_new(request):
    form = PostForm()
    return render(request, 'blog/post_edit.html', {'form': form})

我对此真的很陌生,所以请原谅“简单”的错误。

【问题讨论】:

  • blog_post 模型在哪里定义?它似乎不在您的models.py 中。还有,它用在什么地方?
  • blog_post 没有明确定义。打开网页时出现 blog_post 错误。页面卡住的行是我的 post_list.html 中的{% for post in posts %}
  • 模板从 Django 视图中获取其上下文。显然有一个/app/blog/views.py 文件涉及return render(request, 'blog/post_list.html', {'posts': posts}) 行的错误。请在您的问题中发布此文件的内容。
  • 更新为包含views.py
  • python manage.py make migrations 不是一个有效的推荐!正确的一个是makemigrations(单词之间没有空格)。您在提出问题或进行迁移时打错了字。

标签: django operationalerror


【解决方案1】:

旧帖子,但似乎没有得到答复,所以我会试一试。在遇到类似问题后,我通过谷歌被带到这里。

这个问题可能是 heroku 端缺少数据库。如果您通过 git 推送,并且 db.sqlite3 在您的 .gitignore 中,那么它肯定会在那里丢失,并且需要在那里创建。这正是我在摆弄预制示例(尚未创建数据库的地方)时遇到的问题。

$ python manage.py migrate

你的heroku环境应该修复它。

【讨论】:

    【解决方案2】:

    这是因为在 Heroku 上你很可能已经安装了 postgresql 后端,而不是 sqlite。您需要配置您的生产部署以正确使用 postgresql...我会给出说明,但我现在必须弄清楚...

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,并在 reddit 上找到了解决方案。使用

      git add -f db.sqlite3

      然后在 pythonanywhere.com 上提交、推送,最后拉取。

      感谢 reddit 用户 elbuckez 和牛仔裤_and_a_t-shirt

      【讨论】:

        【解决方案4】:

        我做了两件事来解决这个问题:

        1. 在服务器控制台中输入 python3 manage.py migrate,正如上面的 David Nugent 所说。
        2. 在.gitignore 文件中,我不得不注释掉db.sqlite3。然后我提交并推送了这个更改,然后回到我的服务器(在我的情况下是 pythonanywhere,而不是 heroku)并做了git pull

        【讨论】:

          【解决方案5】:

          在这些命令之后运行这个命令python manage.py migrate --run-syncdb

          1. python manage.py migrate
          2. python manage.py makemigrations

          【讨论】:

            【解决方案6】:

            在你的models.py中包含这个语句:

            from django.contrib.auth.models import User
            

            【讨论】:

            • 我包含了该声明,它没有任何区别。此代码适用于我的虚拟环境,但在我推送到 heroku 后不起作用。另外,在推送到 heroku 之前,我在虚拟环境中更改了管理员密码——这会导致任何问题吗?
            • 更改密码不会有任何问题
            • 你的数据库中是否创建了表 blog_post?
            • 也许我找错地方了。但是在 db.sqplite3 中(在记事本中打开后)我没有找到任何“blog_post”。但是,我的 .gitignore 有 db.sqlite3 所以如果它在那里,它仍然无济于事。另外,如果它在我的虚拟环境中运行,这是否意味着该表已创建,也许它来自我的 .gitignore 或 git 在某处的某个文件中没有看到更改?
            猜你喜欢
            • 2014-03-26
            • 2016-01-05
            • 1970-01-01
            • 1970-01-01
            • 2017-03-12
            • 1970-01-01
            • 2017-03-27
            • 2013-10-21
            • 1970-01-01
            相关资源
            最近更新 更多