您好我正在进行djangogirls教程并且遇到了OperationalError没有这样的表: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
Models.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"),
)
博客/ 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})
我真的很陌生,所以请原谅“简单”的错误。
答案 0 :(得分:5)
老帖子,但似乎没有回答,所以我会开枪。在遇到类似问题之后,我通过谷歌被带到了这里。
这个问题可能是heroku方面缺少的数据库。如果你是通过git推送的,并且db.sqlite3在你的.gitignore中,它肯定会在那里丢失,需要在那里创建。当摆弄一个预先制作的例子(尚未创建数据库)时,这正是我的问题。
$ python manage.py migrate
你的heroku环境中的应该修复它。
答案 1 :(得分:1)
我做了两件事来解决这个问题:
python3 manage.py migrate
。db.sqlite3
。然后,我提交并推送了此更改,然后又回到了我的服务器(在我的示例中为pythonanywhere,而不是heroku),并执行了git pull
。答案 2 :(得分:0)
在models.py中包含此声明:
from django.contrib.auth.models import User
答案 3 :(得分:0)
这是因为在Heroku上你很可能安装了postgresql后端,而不是sqlite。您需要配置您的生产部署以正确使用postgresql ...我会给出相关说明,但我现在必须弄清楚...
答案 4 :(得分:0)
我遇到了同样的问题,并在reddit上找到了解决方案。使用
git add -f db.sqlite3
然后提交,推送,最后拉上pythonanywhere.com。
对reddit用户的信用elbuckez&amp; jeans_and_a_t恤
答案 5 :(得分:0)
在这些命令之后运行此命令python manage.py migrate --run-syncdb
python manage.py migrate
python manage.py makemigrations