Getting DatabaseError, no such table with sqlite3 - django

First time deploying a project. Getting DatabaseError when navigating to a view that modifies or creates Objects in the db. The error I get is:
DatabaseError at /uap_app/coach/request/
no such table: uap_app_coachrequest
with Traceback found here:
Suggestions include ensuring that settings.py reflects the full path for the db, which I have already done. Additionally, I have checked and ensured that all of the tables have been properly created by querying within the sqlite env. Tables initially created via manage.py sql uap_app and syncdb
in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '/var/www/cscc/uap_prod_db2.sqlite3',
Please let me know if there is anything else I should add?
Setup: Django1.4, Python2.7, Apache2.2, mod_wsgi, sqlite3, CentOS

Got it! My machine had multiple python versions installed so I needed to ensure that it pointed to the python2.7 executable instead of the default system-wide installed python.
added this line to httpd.conf:
WSGIPythonExecutable \usr\local\bin\python2.7
got an error from apache saying Invalid command 'WSGIPythonExecutable'-- apparently used only for a previous version of mod_wsgi 1.x (as per this post)
so then i tried adding this line to httpd.conf:
WSGIPythonExecutable \usr\local
and voila! It worked!
Thanks to unsorted for additional help

Related

Django sphinx documentation does not read environment variables in settings file

I want to document my cookiecutter django project with sphinx. The problem is that when running make html sphinx gives me problems reading the config file. It says django.core.exceptions.ImproperlyConfigured: Set the USE_DOCKER environment variable
When not calling django.setup() it also throws me an error with my envs: django.core.exceptions.ImproperlyConfigured: Set the POSTGRES_DB environment variable When I hardcode them, the error goes on to complain about the next environment variable. I can't hardcode them all into the config file, that is not an option.
My environment variables are properly configured. When I print them out running my localhost they are there. It seems that somehow sphinx cannot process them. I am also using docker, so maybe that could interfere but I don't know. Here are parts of my sphinx config:
sys.path.insert(0, os.path.abspath('..'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'config.settings.local'
Here are parts of my local settings:
# ------------------------------------------------------------------------------
# https://django-extensions.readthedocs.io/en/latest/installation_instructions.html#configuration
INSTALLED_APPS += ['django_extensions'] # noqa F405
# https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
# 'default': env.db('DATABASE_URL'), # This was the default value, but modification below seemed necessary
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': env("POSTGRES_DB"),
'USER': env("POSTGRES_USER"),
'PASSWORD': env("POSTGRES_PASSWORD"),
'HOST': env("POSTGRES_HOST"),
'PORT': env("POSTGRES_PORT"),
}
}
Grateful for any kind of help. Thanks in advance!
So it turned out that apparantly some applications can't use environ to read from settings from file. Another problem was that docker alpine image doesn't come with make pre installed.
I got it to work by installing make in my docker image (apk add make) and building the doc while spinning up the container with docker-compose -f local.yml run django make -C ./docs html. Thanks goes out to uzi0espil for leading me there.
For more information see:
https://github.com/pydanny/cookiecutter-django/issues/1747
https://github.com/cookiecutter/cookiecutter/issues/1251
I had this same issue, related to the RTD build passing, but not rendering the whole thing properly.
Issue
My code had:
os.environ['MY_ENV_VAR']
The RTD build would pass, but not render any autodoc elements. The imports failed due to not being able to identify the key of ['MY_ENV_VAR'], raising a KeyError(key).
This error can be found by viewing the RAW output of build logs at your RTD account homepage under 'Builds'.
Solution
To resolve this, you can add the required environment keys ("POSTGRES_DB" for example), in your RTD account at Admin/Environment Variables. Here you can add the required keys, and in my experience, you can add nonsense values for the value. The RTD build will now fully pass as it can identify the Key(s) that you passed when calling os.environ[].

Getting error with postgis Geodjango on Heroku

Postgis extension is installed:
:DATABASE=> SELECT postgis_version();
postgis_version
2.2 USE_GEOS=1 USE_PROJ=1 USE_STATS=1
I have the following buildpacks:
https://github.com/cyberdelia/heroku-geo-buildpack.git
https://github.com/heroku/heroku-buildpack-python.git
When I run manage.py migrate I get:
AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type'
I am using the hobby deb postgres which now supports postgis
https://devcenter.heroku.com/changelog-items/792
Do I need to install a different build pack or add some additional configuration? Everything works locally using postgis.
I finally had some time to go back and look at this. It turns out the issue was Heroku was not importing my settings correctly. I was using cookiecutter-django settings scheme that imports common settings into production and for some reason Heroku was not working as expected.
My common settings contained:
DATABASES['default']['ATOMIC_REQUESTS'] = True
DATABASES['default']['ENGINE'] = "django.contrib.gis.db.backends.postgis"
And my production contained:
DATABASES['default'] = env.db("DATABASE_URL")
Heroku did not import those common settings. When I checked in the django shell in heroku the production settings had
'ENGINE': 'django.db.backends.postgresql_psycopg2', 'ATOMIC_REQUESTS': False
After adding DATABASES['default']['ENGINE'] = "django.contrib.gis.db.backends.postgis" to production settings everything is working.
Does anybody know what could be going wrong with importing settings correctly from common.py? It seems to import the rest of the settings correctly, just not the database ones.

Django unable to open database file using mysql

I'm a newbie in django so as python
I just succesfully configured my first django site over an apache server, then I configured it to work with mysql database editing the settings.py file and running the following command
python manage.py syncdb
I started playing a bit with the admin but occasionally when making get or post requests I get the following message OperationalError at "/some/route" unable to open database file
If I refresh the page loads fine, but if i keep refreshing any page in the admin the error shows up, so it's a random thing.
For what I've searched this is an issue related with sqlite, but I'm using mysql.
My database config in settings.py is the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'simulation',
'USER': 'root',
'PASSWORD': 'root',
'HOST': ''
}
}
I tried to specify hosts "localhost" and "127.0.0.1" but the result was the same.
It's really important for me find a solution that uses mysql as database engine.
Any help would be really appreciated! Thanks.
I've encountered a similar problem that you have right here, in my instance it seemed like my settings.py file was cached on the server. Once I deleted the .pyc files it started working fine.
Also, what happens if you change your database to sqlite? It might be worth checking that your mysql database isn't playing up instead of django.
I ran into the same problem with Django/Mysql/WSGI/Apache setup. Similar to Serakiel, I think that the problem stemmed from server caching. Restarting apache fixed the problem for me.

Unable to sync database in Django

I'm trying to create my first web application using Django.
I'm following the instructions given in the official Django documentation. (The first tutorial: https://docs.djangoproject.com/en/1.5/intro/tutorial01/)
The server started successfully, but when I run the following command (from command prompt):
python manage.py syncdb
I'm getting the following error
ORA-12560: TNS: protocol adapter error
I'm able to access Oracle homepage by going to 127.0.0.1:8080/apex/
I have Oracle 10g database express edition installed on my system, along with the Oracle instant client. After doing a Google search for the above error code, I've been presented with a variety of solutions — about adding ORACLE_HOME in environment variables and pointing that to
E:\oraclexe\app\oracle\product\10.2.0\server\bin;
But that doesn't fix the issue.
I have also added
E:\instantclient;E:\oraclexe\app\oracle\product\10.2.0\server;
to the PATH variable.
I have Python 2.7 installed along with cx_Oracle. I have successfully checked the installation of cx_Oracle by importing it in python. Some of the solutions speak about an ORACLE_SID. I'm unable to find that.
As you can see, I'm a complete newbie to stackoverflow and programming. It's really discouraging to come across such a problem in my very first program. So please help me solve this issue.
To use SQLite3, you just need to modify your settings.py DATABASES field to something like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '/path/to/mysite/database.db', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': '',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
Yo do not need to download SQLite explicitly. Django has inbuilt SQLite3 database engine.

how to use manage.py syncdb outside of Django project, such as in Tornado?

I was looking through http://lincolnloop.com/blog/2009/sep/15/using-django-inside-tornado-web-server/ and I thought it was interesting and useful to use parts of Django if we need it in Tornado.
Based on the setup in http://lincolnloop.com/blog/2009/sep/15/using-django-inside-tornado-web-server/ how can we use manage.py syncdb ?
Here's what i have tried so far:
I've tried shifting manage.py to the same folder as the tornado project, and ran manage.py syncdb but it returns saying that settings.py is not found.
than i tried to move setting.py to the same folder and ran manage.py again. It tells me that no fixtures found. This time round, I have no idea how to configure settings.py since this is not a Django project.
Any advice or thoughts?
=================updates======================
Hi all,
continuing from the above an using advice provided by Agos,
i've tried running python manage.py syncdb --settings=dj_tornado and it returns
`"Error: Can't find the file 'settings.py'` in the directory containing 'manage.py'`. It appears you've customized things.
You'll have to run django-admin.py, passing it your settings module.
(If the file settings.py does indeed exist, it's causing an ImportError somehow.)"
So what i did is to run django-admin.py syncdb --settings=dj_tornado and it returns "django.core.exceptions.ImproperlyConfigured: You haven't set the database ENGINE setting yet."
But the weird thing is that the database engine has been set. How would I go about fixing this? i'm using django 1.2.3 and Tornado 0.2 by the way.
=================updates again======================
Hi all,
i've applied the advice provided by Agos, with a settings.py file in teh same folder as manage.py, and ran the command django-admin.py syncdb --settings=dj_tornado.
I still received the error:
django.core.exceptions.ImproperlyConfigured: You haven't set the database ENGINE setting yet.
But i have already configured the database based engine as follows:
in dj_tornado.py:
from django.conf import settings
settings.configure(
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'dev.db',
}
}
)
I'm kind of at my wits end. How do i use syncdb outside of Django project?
Best.
If I got it correctly, you can just use the --settings switch to point manage.py to the dj_tornado.py, which is your settings file after all
Update 1
from the help, available at python manage.py help:
Options:
--settings=SETTINGS The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't provided, the
DJANGO_SETTINGS_MODULE environment variable will be
used.
So I would try this:
python manage.py syncdb --settings=dj_tornado
Update 2
Another error, another update to the answer!
First of all, consider that that blog post is quite old (september 2009). Django's DATABASES setting has been updated since 1.2.
The syntax in the blog post was:
settings.configure(DATABASE_ENGINE='sqlite3', DATABASE_NAME='dev.db')
With Django 1.2.X this is surely not correct. This would be the equivalent version:
settings.configure(DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'dev.db'
}
})
(sorry for the horrible formatting of the code).
If this still won't work, I'd consider creating a “standard” Django settings file to import. But my bet is on the db settings syntax.
Last update, I swear
Have you tried using django-admin.py again with the new syntax? If so, and still didn't work, a minimal settings.py would be just this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'dev.db'
}
}
You can also keep the original configuration inside dj_tornado.py and use settings.py just to do syncdb.