Django unable to open database file using mysql - django

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.

Related

How to change the database local to be in a cloud (Django)

I wrote a project in django framework and I use the local DataBase SQLite, and I now want to upload it to the cloud so other people who work with me can access DataBase i.e. when I create a user then that user will exist with my co-workers.
Maybe someone has a guide on how to do this without modifying the DataBase so that I do not have to modify the rest of the code?
Ok i found the answer,
what you should do is Connect Django and MongoDB Using Djongo
you can read from here
I RECOMMEND TO FOLLOW MY SETPS.
go to monogodb site -> clickHere and create free account.
create new cluster
then you will reverse to this page if no in the left side press DataBase under DEPLOYMENT and then you get.
press on Connect buttom
choose "Connect your application" and then choose python and the last version
after all this you will have this link:
for example!!!
mongodb+srv://<username>:<password>#<atlas cluster>/<myFirstDatabase>?retryWrites=true&w=majority
copy this link and do the next setps:
go back to your terminal and install this package: pip install djongo
and open settings.py
change your DATEBASE to like this:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'your-db-name',
'CLIENT': {
'host': 'mongodb+srv://<username>:<password>#<atlascluster>/<myFirstDatabase>?retryWrites=true&w=majority'
}
}
}
in 'host' paste the link you copy from monogodb site.
in 'NAME' write your name of the database.
Now that we have the Django project (and app), you can create the collections in MongoDB using the commands:
python manage.py makemigrations <app-name>
python manage.py migrate
if you get error like 'django.db.utils.databaseerror'
what you should to do is:
It deletes all the files that are in the migrations folder that are in the apps you have created and after that run again the command above.

Why is my Django object deleted with no reason?

I know this question may not be in appropriate shape, but I have no other way to describe it.
I have a Django project, deployed with Heroku. I'm using Postgres add-on. The problem is, few of the objects that my users created is being deleted for some reason. I tried to see if there's a pattern with this issue, but I haven't found one. It happens randomly. (This usually happens when I share(copy+paste) a user's post to other people, but I'm not sure if it's a pattern.)
I know it's not the heroku ephemeral filesystem issue, because I'm using the Postgres add-on. So I started wondering if there could be something wrong with Heroku or the Postgres add-on itself. This is a quite serious issue, but I can't figure out what the problem is.
Has anyone experienced similar issue or knows the answer to this?
Thank you very much.UpdateSo I've checked my settings.py, and I have this :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME' : str(BASE_DIR / 'db.sqlite3'),
}
}
...
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
However, I'm using Postgres add-on in my Heroku..

Data always resets to original state on heroku free account after some time

I have deployed my django app into heroku free account.(first time)
In my working environment I use SQLLite on Heroku I assume POstgres should work.
However I am confused whats going on.
So first of all the app is up and running the data that was in my SQLlite database is currently shows at my deployed app.
Is it because my SQLLite DB was copied to heroku and this is what I actually see?
Another issue that indicates somthing is wron with DB is that if I create superuser via terminal I cant log with it to my app.
Another issue is that it looks data constantly restores to its original state after some time .
I followed the instructions and this is what I have in my settings for DB
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
Because that is how Heroku works. The file system is ephemeral and you must not store anything on it; but sqlite does store is data there, so it will get reset every time a new dyno is started.
Your reference to postgres is confusing. You are not using postgres, but you must.

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.

Getting DatabaseError, no such table with sqlite3

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