Forums

sqlite3 database file working in local console but not in live app

App giving an error 500 and error log saying :

c.execute('SELECT id,title,article FROM blog order by id DESC') OperationalError: no such table: blog

but there is a table blog and I was able to run and get result of the above command from local console. what did I miss. I restarted the server a million times and still not working.

You're probably using a relative path to the database file, so the database that you're looking at and the database that the web app is looking at are not the same. Use an absolute path (like /home/dinnu93/my.db rather than my.db) and it should work.

I'm having issue similar to OP (database failed to work in live app), but there was no error message in my case. Specifically, my page display correctly except the template operator failed to populate the page with my data entry. I checked the admin page, but all the data entry were there. What did I do wrong?

It's impossible to tell what you did from the details you gave. Have you checked that you're using the a full path to your database?

This is what I used:

""
Django settings for mysite project.

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

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

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

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

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '2)z6gao=dn=csifbos_a&8sr5h6#%%imru@l=y*elf)e+c*3ta'

# SECURITY WARNING: don't run with debug turned on in production!
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',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'mysite.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 = 'mysite.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '/home/cqcum6er/my-first-blog/db.sqlite3',
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.8/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/1.8/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

The 'NAME' in DATABASES was changed from a (relative?) path of os.path.join(BASE_DIR, 'db.sqlite3') earlier, and that also did not work.

It looks like that's correct and also, the setting of BASE_DIR would have given the same value, both would work. If you're seeing the entry in Django admin, then the problem isn't with your settings, it's with the code that is rendering the page. You're probably not passing anything into the render of the template or maybe you're passing the wrong thing in.

I have the following for my views.py:

from django.shortcuts import render
from django.utils import timezone
from .models import Post  #'Post' table is retrieved from 'models.py' within the same folder.

def post_list(request):  #"post_list" must be requested from urls.py
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'blog/post_list.html', {'posts': posts})

The 'post_list.html' is contained within 'my-first-blog/blog/templates/blog' directory (with the 1st blog directory containing models.py, views.py, admin.py, etc...), but I see nothing wrong with the second argument in render() function. Surely everything was passed correctly to 'post_list.html'?

Why don't you write a simple view function that simply returns "str(posts)", so you can check that "posts" contains what you think it does.

Thanks for the suggestion. After digging around, both posts = Post.objects.values() and posts = Post.objects.filter() gave me the correct results.

Glad I was able to help :-)