Django Newbie: python manage.py syncdb error? - django

Working on the Django tutorial at the moment and got this error when I tried:
python manage.py syncdb
Using sqlite3
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/Library/Python/2.6/site-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
cursor = connection.cursor()
File "/Library/Python/2.6/site-packages/django/db/backends/__init__.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/Library/Python/2.6/site-packages/django/db/backends/sqlite3/base.py", line 281, in _cursor
self._sqlite_create_connection()
File "/Library/Python/2.6/site-packages/django/db/backends/sqlite3/base.py", line 271, in _sqlite_create_connection
self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file
My settings.py file reads:
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '/Users/username/django_tutorial/mysite',
Anyone know the problem here? it seems like I am putting in the complete file directory, but the databases still couldn't be created? ):

You should use the path to the file, including the file name:
'NAME': '/Users/username/django_tutorial/mysite/data.sqlite',
You can also just give it the file name:
'NAME': 'data.sqlite',
In that case it will be created in the same directory than the one from which you run manage.py.
The best practice is to pass an absolute path, but to ensure this absolute path is created relatively to the settings file. E.G:
import os
SETTINGS_DIR = os.path.dirname(os.path.abspath(__file__))
Then latter:
'NAME': os.path.join(SETTINGS_DIR, 'data.sqlite'),
Remember that the settings file is a Python file, and that you can use Python code in it.

There is a list of Django newbie mistakes on project's wiki. See this entry: https://code.djangoproject.com/wiki/NewbieMistakes#DjangosaysUnabletoOpenDatabaseFilewhenusingSQLite3
This specific error is common mistake among Django's newbies. See detailed description below.
PROBLEM
You're using SQLite3, your DATABASE_NAME is set to the
database file's full path, the database file is writeable by Apache,
but you still get the above error.
SOLUTION
Make sure Apache can also write to the parent directory of
the database. SQLite needs to be able to write to this directory.
Make sure each folder of your database file's full path does not start
with number, eg. /www/4myweb/db (observed on Windows 2000).
If DATABASE_NAME is set to something like
'/Users/yourname/Sites/mydjangoproject/db/db', make sure you've
created the 'db' directory first.
Make sure your /tmp directory is world-writable (an unlikely cause as
other thing on your system will also not work). ls /tmp -ald should
produce drwxrwxrwt ....
Make sure the path to the database specified in settings.py is a full
path.
If you working on windows make also sure that you have the path to the
db directory written with double backlashes
'C:\\django\\sqlite\\django.db'

i dont think you need the full path for the name, just put the file name ie "my_db.db". that is assuming you have sqlite3 installed.

Related

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY sett ing must not be empty

I am forking a free Django project on git. The files have no Secret_Key- as you can appreciate.
It has a env folder with files and env.md.
The env.md request to enter all the Values:
Kindly add below environment variables to your local development with appropriate key/value accordingly.
Common keys
DEBUG=True #false
SECRET_KEY="(I have entered here, a python random generated 50 character as the Secret_key)"
HTML_MINIFY=True/False
ENV_TYPE="DEV" or "PROD"
File "/opt/miniconda3/lib/python3.7/site-packages/django/core/
management/init.py", line 401, in execute_from_command_line utility.execute()
File "/opt/miniconda3/lib/python3.7/site-packages/django/core/
management/init.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/miniconda3/lib/python3.7/site-packages/django/core/
management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/opt/miniconda3/lib/python3.7/site-packages/django/core/
management/commands/runserver.py", line 61, in execute
super().execute(*args, **options)
File "/opt/miniconda3/lib/python3.7/site-packages/django/core/
management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/opt/miniconda3/lib/python3.7/site-packages/django/core/
management/commands/runserver.py", line 68, in handle
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
File "/opt/miniconda3/lib/python3.7/site-packages/django/conf/
init.py", line 83, in getattr
self._setup(name)
File "/opt/miniconda3/lib/python3.7/site-packages/django/conf/
init.py", line 70, in _setup
self._wrapped = Settings(settings_module)
File "/opt/miniconda3/lib/python3.7/site-packages/django/conf/
init.py", line 196, in init
raise ImproperlyConfigured("The SECRET_KEY setting must not
be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY sett
ing must not be empty.
I have created a ´random python´ Secret_key as I expect you don't leave your Secret:_key , debug and Database on the git.
what i´ve done_:
I found the env.md and entered a random Secret_key in the Key Value as above.
I ran runserver but it still has same error.
The next thing done:
I also found in the sitepackages/..Django/conf/global_settings .
and pasted also the random generated python Secret_ key as it was blank.
I did runserver but it still says the same error.
Can you advise ?
How come after entering the random Secret_Key I get the same error above.
If I have a project to go to git or production ive read you use python decouple or setup the django environment to do with base.py. but as this is using someones free git with missing secret key. I wondered if anyone has managed to resolve that...
Thank you for advises.
You need to add the secret key you have created, to your settings.py. Let's assume your project is MyProject, your settings.py should be in this directory:
MyProject:/
MyProject:/
settings.py
You should add this line:
# settings.py
SECRET_KEY="ENTER HERE"
A better practice is, creating a settings_local.py and put your sensitive settings (e.g. secret keys, passwords etc) in this file. And after that in your original settings.py at the last line, you should import this file. Like:
# settings_local.py
SECRET_KEY="ENTER HERE"
# settings.py
from settings_local import *
This file also should be ignored by git (e.g put it in your .gitignore) so that you dont push your secret information on git.

Python manage.py collectstatic TypeError: an integer is required (got type str)

I'm working with Django version 2.2 . When I run the command
python manage.py collectstatic
I get the following log on bash terminal.
You have requested to collect static files at the destination
location as specified in your settings:
/home/djapp/poorface/staticfiles
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 188, in handle
collected = self.collect()
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect
handler(path, prefixed_path, storage)
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 353, in copy_file
self.storage.save(prefixed_path, source_file)
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/files/storage.py", line 49, in save
return self._save(name, content)
File "/home/amit/.virtualenvs/hola/local/lib/python3.6/site-packages/django/core/files/storage.py", line 288, in _save
os.chmod(full_path, self.file_permissions_mode)
TypeError: an integer is required (got type str)
I changed the permissions of the staticfiles to -rwxrwxrwx . Then also there is no change in output.
manage.py looks like
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Poorface.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
This is a hack - not a proper fix!
I had this problem on both OSX and Ubuntu. Seems that certain packages end up with file permission as string, whereas most are integer.
I hacked django to fix this and identified the package causing the problem - django-filebrowser - probably best not to hack django but if desperate here is the change in core/files/storage.py line 289::
if self.file_permissions_mode is not None:
if type(self.file_permissions_mode) == type("duck"):
print(full_path, self.file_permissions_mode, type(self.file_permissions_mode))
self.file_permissions_mode = int(self.file_permissions_mode)
os.chmod(full_path, self.file_permissions_mode)
This reported::
/home/django/teamup/shared_static/filebrowser/css/filebrowser.css 0644 <class 'str'>
I tried changing the file permissions for filebrowser in place in the virtualenv and rerunning but this did not fix the problem so I've gone with the hack.
Yes this question is old but I run into this now again and found the error-source and the proper fix after some tufts of grey hair, tampering all sorts of of file-access-rights and one simple debug-run of "manage.py collectstatic":
In the olden days or maybe in some different context (can't remember how this crept in to my project-dev-settings - in my production setting it was correct) it is/was fine to set the "FILE_UPLOAD_PERMISSIONS" in the settings.py like this:
FILE_UPLOAD_PERMISSIONS = "0644"
According to the docs at least since Django 1.8 the correct setting is:
FILE_UPLOAD_PERMISSIONS = 0o644
after I knew the reason an the solution I also found a few hints that's "now with python 3 … octals … etc. etc." but no connection to this enervating vague error message:
… os.chmod(full_path, self.file_permissions_mode)
TypeError: an integer is required (got type str)
after I used a command python manage.py collectstatic which "allways™" (I know) worked.
So I hope you don't get this much grey hair over some slightly stale projects like me!

ValueError: path is on mount 'C:', start on mount 'F:' while django migrations in windows

I am trying to run the following command
python manage.py makemigrations
But, getting the error
ValueError: path is on mount 'C:', start on mount 'F:'
What can be the reason?
complete traceback:-
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 345, in execute
output = self.handle(*args, **options)
File "C:\Python34\lib\site-packages\django\core\management\commands\makemigrations.py", line 189, in handle
self.write_migration_files(changes)
File "C:\Python34\lib\site-packages\django\core\management\commands\makemigrations.py", line 207, in write_migration_files
migration_string = os.path.relpath(writer.path)
File "C:\Python34\lib\ntpath.py", line 579, in relpath
raise ValueError(error)
ValueError: path is on mount 'C:', start on mount 'F:'
The error is occurring because Django is trying to find a relative path between 2 directories and they do not exist (since they are on different drives). This issue is mentioned here: https://bugs.python.org/issue7195. But this response helps to understand the problem: https://bugs.python.org/msg94780.
os.relpath does give you a relative path between two directories.
The problem you are encountering is that, on Windows, a relative path
doesn't even exist if the two directories are on different drives
(which is exactly what the error message says).
When I ran into this error, I was trying to make a migration file for my own app that is located on the F: drive. Running:
python.exe .\manage.py makemigrations
had Django scanning for every change. It located a change for djcelery. This is located in a virtual environment on my C: drive.
To fix this, I just added the app name when calling makemigrations:
python.exe .\manage.py makemigrations <<app_name>>

Django Database Improperly Configured when function called outside of Django

I'm trying to call a python function that makes some queries into my django database from GNU mailman.
When mailman tries to deliver a message, it imports my python script. It later calls a function in my script to modify the message object. The error I'm getting is:
ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation \
for more details.
Here's how I'm configuring the settings, at the very top of my file:
from django.core.management import setup_environ
from mysite import settings
setup_environ(settings)
When I run python manage.py syncdb, it seems to create the database fine. Here's my database configuration:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'django_db', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'root', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
Further, I've commented out the entirety of my function such that it now looks like:
def f():
return
So I don't think this has to do with the function call.
Further, I've tested the setup_environ lines in the python console and everything works as expected.
Further, when I restart GNU mailman, I believe it has to load all its scripts, which means it necessarily has to import my file. This means that these "setup_environ" lines run when I restart mailman. And it's fine -- I get no errors.
It's only when GNU mailman tries to deliver a message that I have problems.
So I'm pretty stumped. I do run the mailman restart command as sudo with additional PYTHONPATH and DJANGO_SETTINGS_MODULE environmental variables, but I've manually added to the relevant parts to my sys.path and os.environ dict, which doesn't fix the problem either. Besides, the error doesn't suggest it's a problem with the path or being unable to find the settings module.
The full stack trace is:
Jun 04 12:06:11 2012 (5249) Uncaught runner exception: settings.DATABASES is improperly configured. Please supply the ENGINE val\
ue. Check settings documentation for more details.
Jun 04 12:06:11 2012 (5249) Traceback (most recent call last):
File "/var/lib/mailman/Mailman/Queue/Runner.py", line 100, in _oneloop
msg, msgdata = self._switchboard.dequeue(filebase)
File "/var/lib/mailman/Mailman/Queue/Switchboard.py", line 173, in dequeue
redirect_list(msg, data)
File "/home/ubuntu/djcode/mysite/mysite/apps/mailman/redirect.py", line 32, in redirect_list
File "/home/ubuntu/djcode/mysite/mysite/apps/mailman/redirect.py", line 45, in _get_real_listname
from mysite.apps.common.models import CustomUser
File "/home/ubuntu/djcode/mysite/mysite/apps/common/custom_user_manager.py", line 54, in get
email_object = Email.objects.get(email=kwargs['email'])
File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 131, in get
return self.get_query_set().get(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 361, in get
num = len(clone)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 85, in __len__
self._result_cache = list(self.iterator())
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 291, in iterator
for row in compiler.results_iter():
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 763, in results_iter
for rows in self.execute_sql(MULTI):
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 808, in execute_sql
sql, params = self.as_sql()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 71, in as_sql
out_cols = self.get_columns(with_col_aliases)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 218, in get_columns
col_aliases)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 306, in get_default_columns
r = '%s.%s' % (qn(alias), qn2(field.column))
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 49, in quote_name_unless_alias
r = self.connection.ops.quote_name(name)
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/dummy/base.py", line 15, in complain
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation \
for more details.
Seems you have not specified anything in your setting.py file's DATABASES dictionary.Specify the following to connect to databse successfully(as per docs),
1) ENGINE -- Either 'django.db.backends.postgresql_psycopg2', 'django.db.backends.mysql', 'django.db.backends.sqlite3' or 'django.db.backends.oracle'. Other backends are also available.
2) NAME -- The name of your database. If you're using SQLite, the database will be a file on your computer; in that case, NAME should be the full absolute path, including filename, of that file. If the file doesn't exist, it will automatically be created when you synchronize the database for the first time (see below).
3) USER -- Your database username (not used for SQLite).
4) PASSWORD -- Your database password (not used for SQLite).
5) HOST -- The host your database is on. Leave this as an empty string if your database server is on the same physical machine (not used for SQLite).
(OR) click HERE

Django beginner problem: manage.py dbsync

I'm running ubuntu 9.04 32b and got django from Synaptics.
My settings.py is configured for a sqlite3 database.
I've been through this tutorial and got the following error when trying to run the command python manage.py syncdb :
Traceback (most recent call last):
File "manage.py", line 11, in
execute_manager(settings)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 340, in execute_manager
utility.execute()
File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 295, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 192, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 219, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 348, in handle
return self.handle_noargs(**options)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/commands/syncdb.py", line 51, in handle_noargs
cursor = connection.cursor()
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/__init__.py", line 56, in cursor
cursor = self._cursor(settings)
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/sqlite3/base.py", line 145, in _cursor
self.connection = Database.connect(**kwargs)
sqlite3.OperationalError: unable to open database file
Do anyone have a clue on my problem ?
In settings.py are you using a relative path to the sqlite file?
If you are, try changing that to an absolute path.
i.e. instead of:
~/project/mydata.db
use
/home/user/project/mydata.db
This can also happen if your database name is the same as your project name. To fix it just change the name of your db e.g. by adding a .db to the NAME. So if your DATABASE NAME in settings.py was 'epic_project' change it to 'epic_project.db'
Long and boring explanation:
If you start your project by running:
django startproject epic_project
your folder structure will be like this:
/path/to/epic_project/
manage.py
epic_project/
settings.py
if then in your settings.py you set your database as:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'epic_project',
...
}
}
when
python manage.py syncdb
runs it tries to open or create a sqlite db file at /path/to/epic_project/epic_project, but there is a directory there, so it says "oh ok the path exists already, let's open it as an sqlite db", unfortunately for sqlite it's a directory and not a DB, so it cries and django presents these tears to you as "sqlite3.OperationalError: unable to open database file"
might be a permission problem, does your user have sufficient right to write on the folder? for example if you do
sudo python manage.py syncdb
instead, does it work?
For Google's sake:
The path to the database must be the full path to the file --- not just the directory where the file lives.
I had the same problem on Windows then realized that syncdb would not create the specified folder if it didn't already exist. I had specified c:/mysite/db/sqlite3.db in settings but the /db/ folder didn't exist. Created it in terminal then re-ran syncdb successfully.
I had the same problem two days ago. I solved it by setting the 'NAME' in the DATABASE dictionary (settings.py) to the absolute path.
For example.
settings.py
DATABASES = {
'default' : {
'ENGINE' : 'django.db.backends.sqlite3',
'NAME' : DATABASE_PATH,
}
}
here you can set the DATABASE_PATH at the top of the settings.py as such
(Not sure if this will work for windows)
SETTINGS_DIR = os.path.dirname(__file__)
PROJECT_PATH = os.path.abspath(os.path.join(SETTINGS_DIR, os.pardir))
DATABASE_PATH = os.path.abspath(os.path.join(PROJECT_PATH, 'your-database-name'))
For Windows you might have to use the replace method. (Not sure .. But you can try it out as follows)
PROJECT_PATH = os.path.abspath(os.path.join(SETTINGS_DIR, os.pardir).replace('\\', '/'))
Same goes for the DATABASE_PATH.
PS. Correct me if I am wrong.
Similar to answer from user104264 - different perspective...
Currently following the YouTube tutorial I had the same error. Seems to me while running the manage.py in a virtualenv django project directory ...path to virtual env.../django_project/, the database name inside /myapp/settings.py was simply 'storage.db' so
(django-v)...path to virtual env project.../django_project/>python manage.py syncdb
created ...path to virtual env project.../django_project/storage.db
-Bill
If you are specifying a full path to db file and are still having issues, try putting an r before the string.
ie.
r'C:\home\usr\mysite\sqlite3.db'
The problem I was running into was a bootstrapping issue where some model lookups were being done at import (outside of any class or function). Per the docs, this is a bad idea.