Django with PythonAnywhere -- Operational Error no such table - django

I am trying to run my django project on PythonAnywhere and keep getting the error
"OperationalError at /
no such table: analysis_predictions"
when I go to my site.
I am using sqlite3 and python 2.7. It seems like this is a common error and I have followed a bunch of instructions to try to fix it including adding the full file path to my database settings. When I try to run python manage.py migrate in the pythonanywhere bash console I get the error "OperationalError unable to open database file".
Settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME':'/Users/Dahlia/learning_python/scifairserver/db.sqlite3',
}
}
Pythonanywhere console:
img1
Current site:
img2

As you can see from that screenshot, the path on Pythonanywhere is /home/dahlia/scifair, not /users/Dahlia/learning_python/scifair.
You shouldn't hard-code the path at all. Instead, use the BASE_DIR variable to calculate it:
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

Related

Test database for Django + Heroku. Error creating the test database: permission denied to create database

I'm trying to run the tests for my Django project. I wrote this project some time ago, I had different settings then and tests were passing. Now I changed settings and deployed it on Heroku with Heroku Postgres database. Everything works fine already except I can't run tests. I've tried many different settings and nothing worked. Most of the time I'm getting this error: permission denied to create database
My last setting is following the instruction from this article on medium
Basically I have added 2nd Heroku Postgres database, add settings like below (but with valid variables of my heroku databases):
if 'test' in sys.argv:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'd7osdssadag0ugv5',
'USER': 'lhwwasadqlgjra',
'PASSWORD': '1524f48a2ce41177c4ssdadasd3a11680b735302d14979d312ff36',
'HOST': 'ec2-54-75-2326-118.eu-west-1.compute.amazonaws.com',
'PORT': 5432,
'TEST': {
'NAME': 'd7osdssadag0ugv5',
}
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'd7hasadas9hqts5',
'USER': 'nybkduadsdgqot',
'PASSWORD': 'bb535b9cdsfsdfdsfdsfac54851f267444dd8cc230b2a786ab9f446',
'HOST': 'ec2-54-247-132-38.eu-west-1.compute.amazonaws.com',
'PORT': 5432,
'TEST': {
'NAME': 'd7hasadas9hqts5',
}
}
}
Then run python manage.py test --keepdb in my venv. Then I get an error:
RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the first PostgreSQL database instead.
RuntimeWarning
Got an error creating the test database: permission denied to create database
I have also tried what is advised in this article
Do you have any ideas what I could do about this error? I don't know Django well. I play with it from time to time.
I'm using: Python 3.6.9, Django 3.0.3, Heroku Postgresql Hobby Dev
EDIT:
I'm not sure if this is now an issue with my settings DATABASES.
Now when I commented out all my settings concerning DATABASES and I run python manage.py runserver my development server starts as normal and I have access to a database I set before (even after restarting a computer). This looks like actual settings don't have effect (??) Any thoughts?
Django version 3.0.3, using settings 'forumproject.settings'
Starting development server at http://127.0.0.1:8000/
Ok, I found out what it was. My database settings were not taken into account, even I had DEBUG=True because I had this line on the end of the settings:
# Activate Django-Heroku.
django_heroku.settings(locals())
After commenting this out the error permission denied to create database
goes away and I can run tests with
python manage.py test --keepdb
I'm surprised how always posting a question on stack overflow help me to find answer immediately after. I was running in circles

django 2.1 + PostgreSQL 11 + Python 3.7 - Cannot do makemigrations

I'm trying to create a geospatial database with geodjango and postgis following the recommendations of the book : Python Geospatial development, 3rd Edition of Erik Westra, in order to do it I'm trying to configure my django database and to connect it to my PostgreSQL db.
After having launched my PostgreSQL database, I've created my django project and django apps. From then I'd like to apply makemigrations command to my shared app with :
python manage.py makemigrations shared
But then I've go the following error :
File "C:\Users\[...]\Anaconda3\lib\site-packages\psycopg2\__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError
I've even tried to check migrations with showmigrations but it makes the same error message so I've absolutely no clue what's going on.
here's my settings.py file:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'shapeeditor',
'USER': 'shapeeditor',
'PASSWORD': '(password)',
}
}
I've put (password) to hide the real one but I've checked it's the good one.
The NAME corresponds to the database name with a USER who has the same name
The shared app is written in INSTALLED_APPS so I've checked I didn't forget it.
I've looked at the many posts in StackOverflow about the error I got but it doesn't correspond to what I've facing here
After writing my own question I've found where it bugged...
my USER shapeeditor didn't have the privileges, so it couldn't work, just switched with postgres and I worked, I'll have to change privileges in order to make it work

Running django with a sqlite in memory database

I am using Robot Framework for my acceptance testing.
To start the django server I run the python manage.py runserver command from the RobotFramework. After that, I call python manage.py migrate. But the tests are slow because is not using an in-memory database.
I tried creating a new setting file called testing and I an using the following configuration:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
}
}
and setting the enviroment variable DJANGO_SETTINGS_MODULE at runtime with this config.
I run a migrate followed by a runserver but the database does not exist. I can't figure out why. If I use a physical database it works.
I tried to check how the Django TestCase works to run and create the database but I could not find it.
I try to simulate make migrate before running test cases by calling execute_from_command_line() manually.
So the command I choose is: (removing poetry run if you don't use poetry).
poetry run ./manage.py test --settings=myproject.settings_test
I put this command into GNUMakefile to allow me use make test.
Then inside myproject/settings_test.py:
from .settings import *
ALLOWED_HOSTS = [ '127.0.0.1', ]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
}
}
from django.core.management import execute_from_command_line
execute_from_command_line(['./manage.py', 'migrate'])
You might be able to use the django.db.connection.creation.create_test_db function documented here at the start of your tests and then call django.db.connection.creation.destroy_test_db after completing your tests.

Django settings.DATABASE 'default' value is not set

I have a Django 1.4.3 project using Python 2.7.3 on Ubuntu 12.10. In my entire project, there is only one settings.py file, and in manage.py, I have this line
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings")
Of course, the name of my project is hellodjango, and the settings.py file is in the subfolder hellodjango (they have the same name).
When I type the following line into the console:
python manage.py diffsettings
These are my results for DATABASES:
DATABASES = {'default': {}, 'mioa': {'1': 'this thingy', '2': 'that thingy'}}
The relevant lines in settings.py are these:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'mydatabase'
},
'mioa': {
'1': 'this thingy',
'2': 'that thingy'
}
}
I have the 'mioa' entry in there to demonstrate my problem. For some reason, the 'default' field is not getting set. Does anyone know why this is happening? Or what the solution is?
Help on diffsettings says:
Displays differences between the current settings.py and Django's
default settings
which means, that it show only difference between your value and default one. Seems like your value is completely the same as the default one, so you don't see any difference.
Try another engine and/or database name and you'll see it's changed.

Upload Django app and set database correctly

Hi I want to upload my django app to openshift rhc with git. After push and refresh the mainpage is displayed, however everything which needs user instance is not working.
My error is:
'Ident authentication failed for user "admin"'
...py2.6.egg/django/db/backends/postgresql_psycopg2/base.py in _cursor, line 177
I think database is not connected properly:
in my settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'MY_APP_NAME',
'USER': 'admin',
'PASSWORD': 'MY_PASSWORD',
'HOST': '',
'PORT': '',
}
I didn't do manage.py syncdb, I do not know how to do it. Maybe that is the problem, because superuser is not created ?
What about path to db? On my computer settings.py looks a litle bit different:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': '//home//pachucx//Project//db//sqlite3.db',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
Everything worked fine on the computer.
The difference is that in NAME I am using path to the DB file, where on serwer side I use only name of it. Should I add extension .db, or maybe there is need to add a path I dont know.
Or maybe the problem will be solved just by manage.py syncdb? If it is so, tell me how to do it properly e.g with git
Many thanks.
This is an error that is occurring because of your attempt to access the database that is either not created, or you have invalid username and password credentials in your settings file.
Make sure you configure all the settings correctly. - Check your host, db username and password as well as the port - it could be different from your local box.
In order to sync the database you need to navigate in SSH to your project root and locate the manage.py file. Once you have that run the following command:
python manage.py syncdb
This will either:
Create all the tables,
Give you a nasty error saying your settings for your database are not correct, this is a great error to get as you know you need to look into the settings again and correct the problem.
To test whether or not your server can be initiated run the following command in SSH again
python manage.py runserver
If successful, this will give you a local test environment and should spit out a url to test. Should be localhost:8000 or something similar.
If fails, this will let you know if you have models improperly configured, mainly your settings, or url file it will give an error on so you can double check everything is up.
As for using the database that is on your machine on a machine outside of your network I advise to not do that. Go and create the database on the box you have, and make notes of the host, username, password, database name etc, and go back to start of this answer.
All the best,