There are my post on this query but most of them are for linux. None of them exlicitly for windows
in my application i’m setting up the database (sqlite3 , default in Django). after editing setting.py file of my application (mysite)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'D:/Django_Code/sqlite.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.
}
}
after finding that i have to set DJANGO_SETTINGS_MODULE environment so that Django can know about the database setting. So i set it up like
DJANGO_SETTINGS_MODULE = "D:\Django_Code\mysite\mysite\settings.py"
To cross check the database setup when i issue
>>> from django.db import connection
>>> cursor = connection.cursor()
it says DJANGO_SETTINGS_MODULE environment variable is Undefined.
Need help to set DJANGO_SETTINGS_MODULE correctly.
The easiest way to set
DJANGO_SETTINGS_MODULEin Windows is using thesetcommand from the command prompt. You should also be able to set it via system properties, but you’d need to close and reopen the command prompt for the changes to take effect.You can query the current value of
DJANGO_SETTINGS_MODULEusing thesetcommand as well:In addition, you need to set it to the python module name, not the filename (setting it to the filename will give you an error similar to “Could not import settings ‘C:\temp\testproject\settings.py’ (Is it on sys.path?): Import by filename is not supported.“)
For example,
Then you can run python and the module can be imported.
Note that we’ve also explicitly added the directory containing the django project (
testprojectin this case) tosys.path, which is effectively a list of directories where Python looks for modules. This is necessary because Python imports the settings file as a python module, not a file (as mentioned earlier).If you want an interactive shell to play with Django objects, you can use the
shellmanagement command. In your Django project directory, run the following command:Since you’re on windows, you may have to do
instead, since I’ve personally had issues with python scripts not receiving command line arguments otherwise.
(Note that I have IPython installed and Django is being clever and using that; if you don’t have IPython installed, your shell will look slightly different.)
If you want to run a script with Django, the easiest way is to write a custom management command, which you can then run with an argument to
manage.py.