Dynamic site elements not displaying after Django database change - python

So I recently changed databases on a Django-based website from SQLite to MySQL.
I did it via dumping the data from the SQLite into a json file and then migrating it to the new MySQL db then loaded it using the python manage.py loaddata command.
The site is hosted on pythonanywhere and the tranistion there went flawless, however, on the local copy of the project I have(used to test implementing new features and debug), the dynamic elements on the front page wont load.
I have a dynamic js table with information for various cryptocurrencies.
This is how the head of the table looks on the live site:
And this is how it looks on the local copy of my machine, with my own hosted MySQL server:
What I've tried:
-Collectstatic
-Running ContentType.objects.all().delete() in python manage.py shell
I also did research on stackoverflow but I could not find a similar issue.
I am also new to Django so it might be something obvious I'm missing

Try with Django fixtures.
Here is the official link for Django fixtures

Solved: I used the datadump.json from pythonanywhere rather than my local copy. Also had some issues with the json utf8 encodings so make sure your datadump file is utf8 encoded and/or matches the encoding of your db.

Related

Is there a way to add data to the SQLite DB provided with Django with another python script?

Is there a way to run a python script that will add data to this SQLite DB then? I mean, a completely separate .py file that I will run in the shell, doing python thefile.py in an SSH connection?
Of course, this file will be on the same server as the whole Django project.
This way, I can easily access the data from the DB from my Django project, and display it the way I want in a pretty HTML/CSS web page.
Thanks
Is there a reason for not using django? you can add a command to the manage.py the way it's described here https://docs.djangoproject.com/en/1.11/howto/custom-management-commands/

Strange error during initial database migration of a Django site

I have been working on a localhost copy of my Django website for a little while now, but finally decided it was time to upload it to PythonAnywhere. The site works perfectly on my localhost, but I am getting strange errors when I do the initial migrations for the new site. For example, I get this:
mysql.connector.errors.DatabaseError: 1264: Out of range value for
column 'applied' at row 1
'applied' is not a field in my model, so this error has to be generated by Django making tables for its own use. I have just checked in the MySQL manager for my localhost and the field 'applied' appears to be from the table django_migrations.
Why is Django mishandling setting up tables for its own use? I have dropped and remade the database a number of times, but the errors persist. If anyone has any idea what would cause this I would appreciate your advice very much.
My website front end is still showing the Hello World page and the Admin link comes up with a page does not exist error. At this stage I am going to assume this is related to the database errors.
EDIT: Additional information about why I cannot access the front-end of the site:
It turns out when I am importing a pre-built site into PythonAnywhere, I have to edit my wsgi.py file to point to the application. The trouble now is that I don't know exactly what to put there. When I follow the standard instructions in the PythonAnywhere help files nothing seems to change. There website is also seems to be very short on detailed error messages to help sort it out. Is there perhaps a way to turn off their standard hello world placeholder pages and see server error messages instead?
As it says in my comment above, it turns out that the problem with the database resulted from running an upgrade of Django from 1.8 to 1.9. I had forgotten about this. After rolling my website back to Django 1.8, the database migrations ran correctly.
The reason why I could not access the website turned out to be because I had to edit the wsgi.py file, but I was editing the wrong version. The nginx localhost web server I was using keeps it in the different folder location than PythonAnyhwere's implementation. I uploaded the file from my localhost copy and edited it according to the instructions on PythonAnywhere's help system without realizing it was not being read by PythonAnywhere's server. What I really needed to do was edit the correct file by accessing it through the web tab on their control panel. Once I edited this file, the website front end began to work as expected.
This issue occurred for me as well on version 1.10 with a brand new project. I found that if you use the recommended driver and the connector in the documentation, the migration works without issues.
If you don't feel like reading the docs, in breif:
Install MySQLdb for python 2.7 or mysqlclient for python 3.3+
Modify your settings.py file. In the DATABASES dictionary set:
'ENGINE': 'django.db.backends.mysql',

How can I switch my Django project's database engine from Sqlite to MySQL?

I need help switching my database engine from sqlite to mysql. manage.py datadump is returning the same error that pops up when I try to do anything else with manage.py : ImproperlyConfigured: Error loading MySQL module, No module named MySQLdb.
This django project is a team project. I pulled new changes from bitbucket and our backend has a new configuration. This new configuration needs mysql (and not sqlite) to work. Our lead dev is sleeping right now. I need help so I can get started working again.
Edit: How will I get the data in the sqlite database file into the new MySQL Database?
According to the Database setup paragraph in Django docs:
By default, the configuration uses SQLite.
If you wish to use another database, install the appropriate database
bindings...
And Get your database running page says:
If you’re using MySQL, you’ll need the MySQL-python package, version
1.2.1p2 or higher.
To use MySQL backend you need a tool that would help Django talk to the database, an adapter.
In other words, MySQL-python (aka MySQLdb) package needs to be installed.
Try the followings steps:
1. Change DATABASES in settings.py to MYSQL engine
2. Run $ ./manage.py syncdb

Django unable to open sqlite on SOME queries only?

I have had no troubles locally, but pushing a new project to an existing machine (one running plenty of other django apps without trouble) gave this:
OperationalError: unable to open database file
What is more perplexing is:
The sqlite file is read-write for all
This error only happens on some queries! Other's are fine.
In a fresh db after running syncdb, my views work, but /admin/ triggers this.
If I loaddata for some of my apps from data dumped from my local machine, some of the apps trigger this in my views, and others do not.
I can find no correlation between any of the things that seem to trigger this.
Why would it fail to open the database, aside from permissions?
Write access on the directory. I thought i checked that first :-/

Migrate a SQLite3 database table to MySQL with Python without dump files

I need to migrate information that I created in SQLite3 to a MySQL database on my website. The website is on a hosted server. I have remote access to the MySQL database. I initially thought it would be easy, but I am not finding any good info on it, and everything I read seems to imply that you need to dump the SQLite3 file, convert it to a MySQL dump file using messy scripts, and then import it into the MySQL.
(example: Quick easy way to migrate SQLite3 to MySQL?)
My question: Is it not better to read it and import it straight from the script into MySQL. I haven't used MySQL at all with Python, but it would seem intuitive that it would be better to have less steps for things to be miss-read. I am also trying to save a little time by not having to understand the way that a MySQL dump file works.
If you have a script (Python if possible), tool or link that will help me out that would be great, but my main concern first of all is how to go about doing it. I can't realistically check everything (otherwise I would just do copy and paste), so I want to be sure that I am going about it the right way.
I am using Django, perhaps there is a Django specific method for it, but I haven't been able to find one.
The reason the "messy" scripts are required is that it's generally a difficult problem to solve.
If you are lucky there won't be too many schema incompatibilities between the databases.
These may help
db_dump.py
dbpickle.py
What is your favorite solution for managing database migrations in django?
Have you tried using django-admin's dumpdata and loaddata?
e.g. dump sqllite3 db
manage.py dumpdata
change db to mysql, recreate tables and load
manage.py loaddata mydata.json
dumpdata by default dumps json but you can set format to xml too.
Alternatively, you can just read whole sqlite3 db using python db api or sqlalchemy or django ORM and dump it to MySQL db.

Categories