Forums

Database not working with my Django App

I have followed all instructions provided and deployed my Django app successfully with all thge static files loading correctly. My Django project uses default Sqlite3 to store data which consists of media files and images. Although the site load correctly but all the data stored in the database are not showing on the site. When I checked inside my project main directory, I noticed the Sqlite file is showing grey and not clickable unlike other files like manage.py.

I have installed pillow, run migrations and follow all instructions provided but data is still not loading on the site. I checked log file but can't see any error. I am confused and don't know what to do to fix this issue.

Can you please help? Is there any special way to set up database to work with my django app? Is there anything I am missing out?

Make sure that your code is actually using the file that you want it to use: https://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/

Thank you for your prompt response. However my issue has not yet been fixed. I don't have any such error message like NoSuchFileOrDirectory. The site is loading correctly with all static files but Everything in my site SQlite database is just blank on Pythonanywhere. All the files in my SQlite database are all found inside the right file named "media" inside my project main folder as I uploaded them but they are just loading on the site.

It appears like the SQlite db is disabled because when I opened my project main directory where there is manage.py, requirements.txt and other files, only db.sqlite3 appears grey and is not clickable. Is there any more configuration I need to do inside settings.py or any other step?

This is what I have in my settings.py DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': '/home/adeypx/milestone4/db.sqlite3' } }

MEDIA_URL = '/media/' MEDIA_ROOT = "/home/adeypx/milestone4/media"

Have you actually checked that the data you expect is in that database file? The file is not clickable because you cannot edit it through the web.

As instructed in this link, I used cd to navigate to /home/adeypx/milestone4/ where I have the project directory that contains manage.py and I used this command ./manage.py migrate and I got message that everything is ok. Please tell me how to check if data is in the database file file? Is there any other step that I am missing out? Can you please check my settings.py if anything to add or change?

Django migrate only creates a table structure in your database. What data do you expect there?

The data I expect there include images and audio but everything is blank right now

You can use the sqlite command line tool to read your database and see if it has what you expect in it: https://sqlite.org/cli.html

After reading my databse, I found out theat it does not the data I expect in it. How do I get the data into the database on PythonAnywhere?

Upload the file that has the data or insert the data on PythonAnywhere in the same way that you inserted it locally.

The folder containing the data has already been uploaded along with the main project folder from GitHub. The folder containing the data is found in milestone4/media/ folder. When I opened the folder, all data files are there but they are not showing on the site in PythonAnywhere.

So the issue is the database is empty or that your web app is not reading media files? Where do you want to get the static files from? Also -- did you check your web app's error log again?

I encountered the same issue with my SQLite databases. They functioned perfectly on my computer but failed to retrieve data on PythonAnywhere. My online searches found this post and story matchs but yet no solution.

Upon investigating, I found that on PythonAnywhere, the databases were created in the root folder, while my app was in the 'mysite' folder, unlike my local environment where project folder was the root.

The solution was simple: I uploaded my databases to the root folder on PythonAnywhere, and that resolved the issue.

I'm sharing this in case others run into a similar problem."

Thanks for posting that! Just to clarify what is happening here: if, for your database file, you specify a relative path -- for example, "mydatabase.db", what you are telling Python to do is to use the file in the current working directory.

Locally, you might run your website by using the cd command to navigate to the directory containing the code, then start it from there. That would mean that the working directory would be the directory containing the script, because the cd command changes the working directory.

On PythonAnywhere, you configure the working directory for your website on the "Web" page. So if you use a relative path, it will be relative to that one. By default, the working directory is your home directory, but you can change it to whatever you want.

In general, though, I'd recommend not using relative paths for database files. Normally you would want the database file for a website to be next to the website's code. In Python code, the following will get the directory containing the script in which the code resides:

import os

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

So if you want to say, in your code 'the file called "mydatabase.db" in the same directory as this script', you can do that to get the directory and then:

db_file = os.path.join(my_dir, "mydatabase.db")