6

Hi I am doing the djangogirls tutorial and have come across the OperationalError no such table: blog_post. This works perfect on my virtual environment, but I get the OperationalError after pushing to heroku.

I am using Django 1.7.7. More information on the error is available at: https://girlsblog.herokuapp.com/

I have done python manage.py makemigrations python manage.py migrate initially in the beginning of the tutorial. Then I attempted to do it again and there are "No changes detected" and "No migrations to make"

This is my 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 %}

This is my .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"),
)

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

I am really new to this, so please forgive "simple" mistakes.

djangogirl
  • 81
  • 1
  • 5
  • Where is the `blog_post` model defined? It does not seem to be in your `models.py`. Also, where is it used? – Wtower May 20 '15 at 08:45
  • `blog_post` is not explicitly defined. The blog_post error shows up when I open the webpage. The line that the page gets stuck on is `{% for post in posts %}` in my post_list.html – djangogirl May 20 '15 at 15:23
  • The template gets its context from a Django view. There is clearly a `/app/blog/views.py` file involved in the error with the line `return render(request, 'blog/post_list.html', {'posts': posts})`. Please post the contents of this file in your question. – Wtower May 21 '15 at 07:39
  • updated to include views.py – djangogirl May 21 '15 at 13:04
  • `python manage.py make migrations` is not a valid commend! correct one is [`makemigrations`](https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-makemigrations) (no whitespace between words). You either make typo in asking the question or in making migrations. – Mp0int May 21 '15 at 13:35
  • was a typo. edited above. – djangogirl May 21 '15 at 13:43

6 Answers6

7

Old post, but does not appeared to be answered so I'll take a shot. I was led here via google after having a similar problem.

This issue is probably a missing database on the heroku side. If you are pushing via git, and db.sqlite3 is in your .gitignore, it will definitely be missing there, and will need to be created there. This was exactly my issue when fiddling with a pre-made example (where the db had not yet been created).

$ python manage.py migrate

on your heroku environment should fix it.

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
David Nugent
  • 377
  • 5
  • 7
1

This is because on Heroku you've very likely got the postgresql backend installed, not sqlite. You need to configure your production deployment to correctly use postgresql... I'd give instructions on that but I have to figure it out now...

Alex Hodge
  • 33
  • 3
1

I encountered the same issue and found a solution on reddit. Use

git add -f db.sqlite3

Then commit, push, and finally pull on pythonanywhere.com.

Credits to reddit users elbuckez & jeans_and_a_t-shirt

Chris
  • 61
  • 1
  • 4
1

I did two things to resolve this:

  1. Typed python3 manage.py migrate in server console, as David Nugent above said.
  2. In .gitignore file, I had to comment out db.sqlite3. I then committed and pushed this change, then went back into my server (in my case pythonanywhere, not heroku) and did git pull.
Jules_ewls
  • 117
  • 1
  • 9
0

Run this command python manage.py migrate --run-syncdb after these commands

  1. python manage.py migrate
  2. python manage.py makemigrations
Ramandeep Singh
  • 552
  • 6
  • 11
-1

Include this statement in your models.py:

from django.contrib.auth.models import User
karthikr
  • 97,368
  • 26
  • 197
  • 188
gaurav_kamble
  • 300
  • 1
  • 4
  • 8
  • 1
    I included the statement and it made no difference. This code works on my virtual environment but does not work after I push to heroku. Also, before pushing to heroku I changed the admin password in my virtual environment--- would that have caused any issues? – djangogirl May 20 '15 at 00:22
  • changing password will not make any issues – gaurav_kamble May 20 '15 at 08:43
  • 1
    Is the table blog_post created in your database? – gaurav_kamble May 20 '15 at 08:44
  • Perhaps I am looking in the wrong spot. But in db.sqplite3 (after opening in notepad) I did not find anything "blog_post". However, my .gitignore has db.sqlite3 so if it was there, it still wouldn't help. Also, if it is working on my virtual environment, wouldn't that mean the table is created and perhaps it's something from my .gitignore or git is not seeing changes in some file somewhere? – djangogirl May 20 '15 at 12:18