I have made my searchings but found no answer. Please help!
This is what I do:
(holistic)chris#notebook:~/workspace$ django-admin --version
1.4
(holistic)chris#notebook:~/workspace$ django-admin startproject holistc
(holistic)chris#notebook:~/workspace$ cd holistc/
(holistic)chris#notebook:~/workspace/holistc$ vim settings.py
(holistic)chris#notebook:~/workspace/holistc$ python manage.py startapp main
(holistic)chris#notebook:~/workspace/holistc$ python manage.py syncdb
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/chris/.virtualenvs/holistic/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/home/chris/.virtualenvs/holistic/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/chris/.virtualenvs/holistic/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/chris/.virtualenvs/holistic/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/home/chris/.virtualenvs/holistic/local/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/home/chris/.virtualenvs/holistic/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
cursor = connection.cursor()
File "/home/chris/.virtualenvs/holistic/local/lib/python2.7/site-packages/django/db/backends/dummy/base.py", line 15, in complain
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
and this is my settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'holistic_db', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # 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.
}
}
any ideas whould be appreciated.
Thanks.
In django 1.4 the path of the settings file would be workspace/holistc/holistc/settings.py.
This is different from previous versions of django. You can read about the new project layout here.
Related
I'm trying to connect to a SAP SQL Anywhere database with pyodbc and the FreeTDS ODBC driver.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'mssql_database': {
'ENGINE': 'django_pyodbc',
'NAME': 'blabla',
'USER': 'blabla',
'PASSWORD': 'blabla',
'HOST': '10.65.1.20',
'PORT': '1433',
'OPTIONS': {
'driver': 'FreeTDS',
'host_is_server': True,
},
},
'sybase_database': {
'ENGINE': 'django_pyodbc',
'NAME': 'blabla',
'USER': 'blabla',
'PASSWORD': 'blabla',
'HOST': '10.60.1.6',
'PORT': '2638',
'OPTIONS': {
'driver': 'FreeTDS',
'host_is_server': True,
},
},
}
Connection to mssql_database works. But connection to sybase_database ends with this error message:
django.db.utils.DatabaseError: ('42000', "[42000] [FreeTDS][SQL Server]SQL Anywhere Error -265: Procedure 'SERVERPROPERTY' not found (504) (SQLExecDirectW)")
all traceback
$ python manage.py inspectdb --database=sybase_database
Traceback (most recent call last):
File "/home/pd/packaging/venv/lib/python3.6/site-packages/django_pyodbc/base.py", line 491, in execute
return self.cursor.execute(sql, params)
pyodbc.ProgrammingError: ('42000', "[42000] [FreeTDS][SQL Server]SQL Anywhere Error -265: Procedure 'SERVERPROPERTY' not found (504) (SQLExecDirectW)")
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/core/management/commands/inspectdb.py", line 25, in handle
for line in self.handle_inspection(options):
File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/core/management/commands/inspectdb.py", line 38, in handle_inspection
with connection.cursor() as cursor:
File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 162, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/home/pd/packaging/venv/lib/python3.6/site-packages/django_pyodbc/base.py", line 362, in _cursor
if self.ops.sql_server_ver < 2005:
File "/home/pd/packaging/venv/lib/python3.6/site-packages/django_pyodbc/operations.py", line 138, in _get_sql_server_ver
cur.execute("SELECT CAST(SERVERPROPERTY('ProductVersion') as varchar)")
File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
File "/home/pd/packaging/venv/lib/python3.6/site-packages/django_pyodbc/base.py", line 497, in execute
raise utils.DatabaseError(*e.args)
django.db.utils.DatabaseError: ('42000', "[42000] [FreeTDS][SQL Server]SQL Anywhere Error -265: Procedure 'SERVERPROPERTY' not found (504) (SQLExecDirectW)")
$ pip list
Package Version
------------- -------
Django 1.8
django-pyodbc 1.1.3
pip 21.3.1
pyodbc 4.0.32
setuptools 59.6.0
sqlany-django 1.13
sqlanydb 1.0.11
wheel 0.37.1
When I run this script
# hello_sybase.py
import pyodbc
try:
con = pyodbc.connect('Driver={FreeTDS};'
'Server=10.60.1.6,2638;'
'Database=blabla;'
'uid=blabla;pwd=blabla')
cur = con.cursor()
cur.execute("Select * from Test")
for row in cur.fetchall():
print (row)
cur.close()
con.close()
except Exception as e:
print(str(e))
It works and prints all the rows. Is it at least possible to work with the results (rows) in a django template? It's sufficient to have read only database.
According to the site for django-pyodbc, Sybase is not supported.
The traceback leads to this code, which seems to be SQL Server specific. You'd have to hack around to make that library compatible with your database... but since you say you don't necessarily need Django's ORM stuff, you can absolutely just connect to the secondary database using the raw pyodbc layer, e.g.
import pyodbc
# TODO: move this to `settings`?
CONN_STRING = 'Driver={FreeTDS};Server=10.60.1.6,2638;Database=blabla;uid=blabla;pwd=blabla'
def my_view(request):
with pyodbc.connect(CONN_STRING) as conn:
cur = conn.cursor()
cur.execute('SELECT * FROM test')
rows = list(cur.fetchall())
return render(request, 'my_template.html', {'rows': rows})
I'm having a hard time because my django project.
Who can help?
I'd like to thank you for your help.
set .env in proeject Root path
SECRET_KEY=exampleKey
DEBUG=False
DB_ENGINE=django.db.backends.mysql
DB_NAME=example
DB_USER=admin
...Blah Blah
project settings.py
import os
import environ
env = environ.Env(DEBUG=(bool, False), )
environ.Env.read_env()
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = env('SECRET_KEY')
DEBUG = env('DEBUG')
...Blah Blah
And when run the project
I get an this error message.
Django_workspace/asone/asone/.env doesn't exist - if you're not configuring your environment separately, create one.
"environment separately, create one." % env_file)
raise KeyError(key) from None
KeyError: 'SECRET_KEY'
During handling of the above exception, another exception occurred:
/Django_workspace/asone/venv/lib/python3.7/site-packages/environ/environ.py", line 277, in get_value
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable
I have a hard time because I'm a django beginner.
Who can help? I'd like to thank you for your help.
.P.S
Thank you so much for your answer.
I'm sorry. I'm telling you, there's something I missed out on.
I set up a django-environ package to use the .env in the project.
pip install django-environ
Is there something I'm missing?
What more information will be needed to solve this problem?
.P.S
set up python-dotenv package
pip install python-dotenv.
Change settings.py
import os
from dotenv import load_dotenv
load_dotenv()
...
SECRET_KEY = os.getenv('SECRET_KEY')
DEBUG=os.getenv('DEBUG')
DATABASES = {
'default': {
# 'ENGINE': env('DB_ENGINE'),
# 'NAME': env('DB_NAME'),
# 'USER': env('DB_USER'),
# 'PASSWORD': env('DB_PASSWORD'),
# 'HOST': env('DB_HOST'),
# 'PORT': env('DB_PORT'),
'ENGINE': os.getenv('DB_ENGINE'),
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
'PASSWORD': os.getenv('DB_PASSWORD'),
'HOST': os.getenv('DB_HOST'),
'PORT': os.getenv('DB_PORT'),
}
}
...
and then I met this error message.
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/config.py", line 562, in configure
handler = self.configure_handler(handlers[name])
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/config.py", line 735, in configure_handler
result = factory(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/handlers.py", line 148, in __init__
BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/handlers.py", line 55, in __init__
logging.FileHandler.__init__(self, filename, mode, encoding, delay)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 1087, in __init__
StreamHandler.__init__(self, self._open())
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 1116, in _open
return open(self.baseFilename, self.mode, encoding=self.encoding)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/haemil/Desktop/Back-end workspace/Django_workspace/asone/logs/logfile'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/Users/haemil/Desktop/Back-end workspace/Django_workspace/asone/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/haemil/Desktop/Back-end workspace/Django_workspace/asone/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "/Users/haemil/Desktop/Back-end workspace/Django_workspace/asone/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "/Users/haemil/Desktop/Back-end workspace/Django_workspace/asone/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "/Users/haemil/Desktop/Back-end workspace/Django_workspace/asone/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/haemil/Desktop/Back-end workspace/Django_workspace/asone/venv/lib/python3.7/site-packages/django/__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/Users/haemil/Desktop/Back-end workspace/Django_workspace/asone/venv/lib/python3.7/site-packages/django/utils/log.py", line 75, in configure_logging
logging_config_func(logging_settings)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/config.py", line 799, in dictConfig
dictConfigClass(config).configure()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/config.py", line 570, in configure
'%r' % name) from e
ValueError: Unable to configure handler 'file'
Is there something I'm missing?
There is no environ module in stdlib(standard library of python). So I checked if there is third party module called 'environ' by running pip install environ, which installed it, but when I checked at pypi website, I can't see any valid explanation. I think it's not maintained well.
So I concluded that your environ configuration went a bit odd.
Here's my suggestion
Install python-dotenv module by running pip install python-dotenv. There are many .env module, but this is one of the most popular, well maintained one in my opinion.
Ensure your .env file is in the directory where settings.py is. setting.py will load .env file in the same directory unless specified otherwise
Change your code from this
import os
import environ
env = environ.Env(DEBUG=(bool, False), )
environ.Env.read_env()
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = env('SECRET_KEY')
DEBUG = env('DEBUG')
to this
import os
from dotenv import load_dotenv
load_dotenv()
...
SECRET_KEY = os.getenv('SECRET_KEY')
DEBUG=os.getenv('DEBUG')
...
Change your db setting similar way
DATABASES = {
'default': {
'ENGINE': os.getenv('DB_ENGINE'),
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
# and so on ...
}
}
}
You are running into a KeyError because either:
You aren't putting a SECRET_KEY variable in your .env file.
It can't find your .env file.
From what I can see from your answer, try moving your .env file from your root directory to where it will sit alongside settings.py.
~/Documents/django1/mysite$ python manage.py syncdb
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 263, in fetch_command
app_name = get_commands()[subcommand]
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 109, in get_commands
apps = settings.INSTALLED_APPS
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 48, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 132, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/home/nick/Documents/django1/mysite/mysite/settings.py", line 15
'NAME': '/home/nick/Documents/django1/sqlite.db'
Settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3'
'NAME': '/home/nick/Documents/django1/sqlite3.db' # 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.
}
}
^
You are missing commas (after the ENGINE and NAME items) in your DATABASES dict:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': '/home/nick/Documents/django1/sqlite3.db', # 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.
}
}
I am trying to use Mongodb with in my Django.
Below there are connection settings in settings.py
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_email#example.com'),
)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': '',
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': 27017,
}
}
when I am trying to run python manage.py syncdb I am getting an error like this:
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_from_command_line(sys.argv)
File "/home/sakeer/workspace/entevirtual/lib/python2.7/site-packages/django/core/management/__init__.py", line 429, in execute_from_command_line
utility.execute()
File "/home/sakeer/workspace/entevirtual/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/sakeer/workspace/entevirtual/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/sakeer/workspace/entevirtual/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/home/sakeer/workspace/entevirtual/lib/python2.7/site-packages/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/home/sakeer/workspace/entevirtual/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 59, in handle_noargs
tables = connection.introspection.table_names()
File "/home/sakeer/workspace/entevirtual/lib/python2.7/site-packages/django_mongodb_engine-0.4.0-py2.7.egg/django_mongodb_engine/base.py", line 76, in table_names
File "/home/sakeer/workspace/entevirtual/lib/python2.7/site-packages/django_mongodb_engine-0.4.0-py2.7.egg/django_mongodb_engine/base.py", line 106, in __getattr__
File "/home/sakeer/workspace/entevirtual/lib/python2.7/site-packages/django_mongodb_engine-0.4.0-py2.7.egg/django_mongodb_engine/base.py", line 133, in _connect
File "/home/sakeer/workspace/entevirtual/lib/python2.7/site-packages/pymongo-2.4.2-py2.7-linux-x86_64.egg/pymongo/mongo_client.py", line 1025, in __getitem__
File "/home/sakeer/workspace/entevirtual/lib/python2.7/site-packages/pymongo-2.4.2-py2.7-linux-x86_64.egg/pymongo/mongo_client.py", line 1014, in __getattr__
File "/home/sakeer/workspace/entevirtual/lib/python2.7/site-packages/pymongo-2.4.2-py2.7-linux-x86_64.egg/pymongo/database.py", line 74, in __init__
django.core.exceptions.ImproperlyConfigured: name must be an instance of basestring
i am using django 1.3
thanks in advance
I've never used mongodb with django, but I'll take a stab just by following the trace. You need to add a value to the NAME setting in your DB configuration
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'foobar',
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': 27017,
}
}
The error is being thrown here in pymongo which is created around here via django_mongo_engine
In attempting to run ./manage.py runserver or shell or any other command for that matter, I'm getting the error: You must define a 'default' database.
I'm running this in a virtualenv and settings.py includes DATABASE_NAME, along with Host, Port and Engine. Where is django expecting definition of the default database?
Here's the traceback:
(env)fox-ser01:common wmfox3$ ./manage.py shell
Traceback (most recent call last):
File "./manage.py", line 31, in <module>
execute_manager(settings)
File "/Users/wmfox3/Sites/photo_project/env/src/django/django/core/management/__init__.py", line 442, in execute_manager
utility.execute()
File "/Users/wmfox3/Sites/photo_project/env/src/django/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/wmfox3/Sites/photo_project/env/src/django/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/wmfox3/Sites/photo_project/env/src/django/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/Users/wmfox3/Sites/photo_project/env/src/django/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/Users/wmfox3/Sites/photo_project/env/src/django/django/core/management/commands/shell.py", line 46, in handle_noargs
from django.db.models.loading import get_models
File "/Users/wmfox3/Sites/photo_project/env/src/django/django/db/__init__.py", line 12, in <module>
raise ImproperlyConfigured("You must define a '%s' database" % DEFAULT_DB_ALIAS)
django.core.exceptions.ImproperlyConfigured: You must define a 'default' database
DATABASE_NAME is deprecated since django 1.2 so if you're using newer version, you should use the new way of defining databases:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'mydatabase'
}
}
define db name is settings.py
DATABASE
Below is an example
DATABASES = {
'default': {
'ENGINE': 'mysql',
'NAME': 'xyz', # db name
'USER': 'root',
'PASSWORD': 'password',
'HOST': '',
'PORT': '',
}
}
In settings.DATABASES.