Configurate MSSQL Server with Django - sql-server

I have installed
**appdirs==1.4.3
asgiref==3.2.7
distlib==0.3.0
Django==2.1.15
django-mssql==1.8
django-mssql-backend==2.8.1
django-pyodbc-azure==2.1.0.0
filelock==3.0.12
pyodbc==4.0.30
pytz==2019.3
six==1.14.0
sqlparse==0.3.1
virtualenv==20.0.18
virtualenvwrapper-win==1.2.6**
My settings.py file in the database section looks like this
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'localhost',
'HOST': 'localhost',
'USER': 'sa',
'PASSWORD': 'root*+123456*+789',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
}
}
}
I got the following error after run py manage.py migrate
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 167, in ensure_defaults
conn = self.databases[alias]
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\functional.py", line 37, in get
res = instance.dict[self.name] = self.func(instance)
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 154, in databases
if self._databases[DEFAULT_DB_ALIAS] == {}:
KeyError: 'default'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\base.py", line 101, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\base.py", line 305, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\options.py", line 203, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 199, in __getitem__
self.ensure_defaults(alias)
File "C:\Users\z003vuxz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 169, in ensure_defaults
raise ConnectionDoesNotExist("The connection %s doesn't exist" % alias)
django.db.utils.ConnectionDoesNotExist: The connection default doesn't exist
Can somebody help me?

Your DATABASES-setting is incorrect, see docs for details.
What you need is:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
The configuration for the connection needs to be inside of 'default': {...}, you placed it directly inside of DATABASES.

Related

in Djando,When I tried to import my model into my view.py,an error occured

When I tried to import my model into my view.py:
from django.views import generic
from .models import *
class ProductListView(generic.ListView):
template_name="discover.html"
queryset=Product.objects.all()
it showed an error as follow:
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/Users/yorkli/Desktop/django-projects/gumroad/djgumroad/config/urls.py", line 9, in <module>
from djgumroad.products.views import *
File "/Users/yorkli/Desktop/django-projects/gumroad/djgumroad/djgumroad/products/views.py", line 2, in <module>
from .models import *
File "/Users/yorkli/Desktop/django-projects/gumroad/djgumroad/djgumroad/products/models.py", line 5, in <module>
class Product(models.Model):
File "/Users/yorkli/Desktop/django-projects/gumroad/venv/lib/python3.10/site-packages/django/db/models/base.py", line 132, in __new__
raise RuntimeError(
RuntimeError: Model class djgumroad.products.models.Product doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
but I did register the app in the INSTALLED_APPS:
LOCAL_APPS = [
"djgumroad.users",
# Your stuff: custom apps go here
"tailwind",
"theme",
"django_browser_reload",
"djgumroad.products",
]
# https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
Is there other configurations I missed?
I tried to remove the "from .models import Product",it works well,when I put it back,the error shows again.

kiwi-tcms: kiwi_db restarting loop

I am trying to install kiwi-tcms and when I get to step:
docker exec -it kiwi_web /Kiwi/manage.py initial_setup
D:\path\to\kiwi-tcms>docker exec -it kiwi_web /Kiwi/manage.py initial_setup
Applying migrations:
Traceback (most recent call last):
File "/venv/lib64/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/venv/lib64/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/venv/lib64/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/django/db/backends/mysql/base.py", line 234, in get_new_connection
connection = Database.connect(**conn_params)
File "/venv/lib64/python3.8/site-packages/MySQLdb/init.py", line 130, in Connect
return Connection(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/MySQLdb/connections.py", line 185, in init
super().init(*args, **kwargs2)
MySQLdb._exceptions.OperationalError: (2005, "Unknown MySQL server host 'db' (-2)")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Kiwi/manage.py", line 12, in
execute_from_command_line(sys.argv)
File "/venv/lib64/python3.8/site-packages/django/core/management/init.py", line 419, in execute_from_command_line
utility.execute()
File "/venv/lib64/python3.8/site-packages/django/core/management/init.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/venv/lib64/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/venv/lib64/python3.8/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/venv/lib64/python3.8/site-packages/tcms/core/management/commands/initial_setup.py", line 11, in handle
call_command("migrate", "--verbosity=%i" % kwargs["verbosity"])
File "/venv/lib64/python3.8/site-packages/django/core/management/init.py", line 181, in call_command
return command.execute(*args, **defaults)
File "/venv/lib64/python3.8/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/venv/lib64/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/django/core/management/commands/migrate.py", line 92, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/venv/lib64/python3.8/site-packages/django/db/migrations/executor.py", line 18, in init
self.loader = MigrationLoader(self.connection)
File "/venv/lib64/python3.8/site-packages/django/db/migrations/loader.py", line 53, in init
self.build_graph()
File "/venv/lib64/python3.8/site-packages/django/db/migrations/loader.py", line 220, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/venv/lib64/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
if self.has_table():
File "/venv/lib64/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
with self.connection.cursor() as cursor:
File "/venv/lib64/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
return self._cursor()
File "/venv/lib64/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
self.ensure_connection()
File "/venv/lib64/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/venv/lib64/python3.8/site-packages/django/db/utils.py", line 90, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/venv/lib64/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/venv/lib64/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/venv/lib64/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/django/db/backends/mysql/base.py", line 234, in get_new_connection
connection = Database.connect(**conn_params)
File "/venv/lib64/python3.8/site-packages/MySQLdb/init.py", line 130, in Connect
return Connection(*args, **kwargs)
File "/venv/lib64/python3.8/site-packages/MySQLdb/connections.py", line 185, in init
super().init(*args, **kwargs2)
django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'db' (-2)")
I'm working windows 10. I also have the kiwi_db that is constantly restarted in docker
Unknown MySQL server host 'db' (-2)"
The error message itself is clear enough. Your DB server doesn't seem to be up and running.
I'm working windows 10. I also have the kiwi_db that is constantly restarted in docker
Kiwi TCMS and MySQL/MariaDB are Linux based containers so maybe your Windows host is not capable of running Linux containers in the first place. Refer to Docker's documentation/support on that matter.
This may be of help but fair warning that it's been written by a 3rd party not affiliated with the Kiwi TCMS team:
https://medium.com/#siriwardhane.yuwin/running-kiwi-tcms-as-a-docker-container-in-windows-10-home-82d74b107202

Django 1.11 - python 3.4 // not able to createsuperuser

**(geoenv) C:\Users\Nitish\Desktop\Mtech Project\Stage 8 database start\geosite>python manage.py createsuperuser
You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
Traceback (most recent call last):
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\db\backends\utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\db\backends\sqlite3\base.py", line 328, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: auth_user
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in
execute_from_command_line(sys.argv)
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\core\management__init__.py", line 363, in execute_from_command_line
utility.execute()
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\core\management__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 63, in execute
return super(Command, self).execute(*args, **options)
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\core\management\base.py", line 330, in execute
output = self.handle(*args, **options)
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 96, in handle
default_username = get_default_username()
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\contrib\auth\management__init__.py", line 148, in get_default_username
auth_app.User._default_manager.get(username=default_username)
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\db\models\query.py", line 374, in get
num = len(clone)
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\db\models\query.py", line 232, in len
self._fetch_all()
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\db\models\query.py", line 1105, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\db\models\query.py", line 53, in iter
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\db\models\sql\compiler.py", line 886, in execute_sql
raise original_exception
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\db\models\sql\compiler.py", line 876, in execute_sql
cursor.execute(sql, params)
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\db\backends\utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\db\backends\utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\db\utils.py", line 94, in exit
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\db\backends\utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "C:\Users\Nitish\Desktop\MTECHP~1\STAGE8~1\geoenv\lib\site-packages\django\db\backends\sqlite3\base.py", line 328, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: auth_user
createsuperuser
cmd can only be used after making models and makemigrations and migrate cmd.
I was trying to use it before that.

How to deal with this ERROR (1049, "Unknown database '/users/ohyunjun/work/astral/mysql'")

In the Django settings.py, I set databases option this way
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.path.join(BASE_DIR, 'mysql'),
'USER': 'root',
'PASSWORD': 'sp153426',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
And I executed this command
python manage.py syncdb
But failed with this error
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 415, in handle
return self.handle_noargs(**options)
File "/Library/Python/2.7/site-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
cursor = connection.cursor()
File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 160, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 132, in _cursor
self.ensure_connection()
File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 127, in ensure_connection
self.connect()
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 127, in ensure_connection
self.connect()
File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 115, in connect
self.connection = self.get_new_connection(conn_params)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 435, in get_new_connection
conn = Database.connect(**conn_params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.6-intel.egg/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.6-intel.egg/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (1049, "Unknown database '/users/ohyunjun/work/astral/mysql'")
'NAME' is the name of your database. With MySQL, you need to manually create your database too. Let's say, if you run:
$ mysql -u root -p
mysql> CREATE DATABASE mydb;
Query OK, 1 row affected (0.02 sec)
your configuration should be:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb',
'USER': 'root',
'PASSWORD': 'sp153426',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
Hey man it just required that the database should already be created in MySQL.
I use docker to start mysql.
I changed the database name, then have same error.
After remove docker's volume cache, thing be ok!
docker volume --help
docker volume ls
docker volume rm xx yy

error FileNotOpenedError

I obtain a error when try to write a file in cloud storage
my code:
my_file = files.gs.create('/gs/foo/myfile')
with files.open(my_file, 'a') as f:
f.write('Hello World!')
f.write('Hello World Again!')
Traceback (most recent call last):
File "C:\Datos\proyectos\dropbox\Proyectos\as3\semtable\remote_api\using_datastore.py", line 63, in <module>
f.write('Hello World Again!')
File "C:\Program Files\Google\google_appengine\google\appengine\api\files\file.py", line 281, in __exit__
self.close()
File "C:\Program Files\Google\google_appengine\google\appengine\api\files\file.py", line 275, in close
self._make_rpc_call_with_retry('Close', request, response)
File "C:\Program Files\Google\google_appengine\google\appengine\api\files\file.py", line 388, in _make_rpc_call_with_retry
_make_call(method, request, response)
File "C:\Program Files\Google\google_appengine\google\appengine\api\files\file.py", line 236, in _make_call
_raise_app_error(e)
File "C:\Program Files\Google\google_appengine\google\appengine\api\files\file.py", line 179, in _raise_app_error
raise FileNotOpenedError()
google.appengine.api.files.file.FileNotOpenedError

Resources