syncing sqlite3 for Django tutorial - django

I'm starting the Django tutorials and have virtual environments and django installed. I'm working on a Mac 10.6.8, which has sqlite3 already installed.
I'm working inside a virtual environment. I changed the settings.py file to:
ENGINE -- 'django.db.backends.sqlite3'
I left the NAME -- in the settings.py file blank (i.e. '') because the tutorial said
"If the file doesn't exist, it will automatically be created when you synchronize the database for the first time."
But when I run python manage.py syncdb, I get the following error:
django.core.exceptions.ImproperlyConfigured: Please fill out the database NAME in the settings module before using the database.
Why isn't my file automatically created? Or what do I need to do to solve this problem?
Thank you!

It will be created for you, but you have to specify what name to call it so just set NAME to sqlite.db or something similar. You can also specify a path so that it doesn't clog up your project directory. I usually save the dev database outside of my project.

Related

Pyinstaller is unable to import custom commands of Django 2.1.5

I am using PyInstaller for creating build for my project on ubuntu machine.
I have written one custom commands createsappsuperuser in django to create sime special users. To create the executable file, I am running my manage.spec file and the executable is getting created successfully. The location of that executable is in dist folder.
After that I am running ./dist/manage createsappsuperuser which gives error like -
Unknown command: 'createsappsuperuser'. Did you mean createsuperuser?
Type 'manage help' for usage.
To solve the above issue, I tried adding my cusom command in the dict
in Pyinstaller/loader/rthooks/pyi_rth_django.py file as because pyi_rth_django.py always return some static set of commands.
I have added it like 'createsappsuperuser': 'idmgmt' in the dictionary. Here, idmgmt is my app name.
But the above solutions was valid for upto Django 1.8, and even the file "pyi_rth_django.py" says that it is tested with Django 1.8.
Then how should i do it in Django 2.1.5 ?
Thanks In Advance

Created a brand new Django Project, attempting to run the server generates an Improperly Configured Error

I have been working on a Django Project for a bit, until I took a break. One month later I dug back in to the project and went to run the server. I received an error:
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I figured I must have tweaked something by accident. So I created a brand new Django Project (using a virtual environment), and just went to test the server. I received the same error. I tried the python manage.py shell solution listed in another answers but to no avail.
If it helps I'm on Linux with Django version 2.1.5 and Python 3.6.
Edit:
If anyone encounters something similar I found using python3 manage.py runserver works in place of using django-admin. Per Greg's answer below, I did begin to receive a new error ModuleNotFoundError: No Module named "mysite" exists. I will continue to search for an answer on that front.
Going off of the comments here.
If "env | grep DJANGO_SETTINGS_MODULE" returns empty, it means you have to set an environment variable stating where your settings.py file is located.
This can be done by doing the following:
export DJANGO_SETTINGS_MODULE=mysite.settings
Be sure to replace "mysite" with the name of your app!

django - How to manually set sys.path?

Till now I was making change on my django production server (yes, really really bad :p ). I am wanna to go to a git process, and creating a local test server before deployement. So, I downloaded my python files, and ran a :
python manage.py runserver
hoping and prayed... but it was not enough, I got a nice error :
django.core.exceptions.ImproperlyConfigured: WSGI application 'issc.issc.wsgi.application' could not be loaded; Error importing module: 'No module named issc.wsgi'
I read in the documentation that [manage.py] is created automatically and sets up several key parts :
In addition, manage.py is automatically created in each Django project. manage.py is a thin wrapper around django-admin.py that takes care of several things for you before delegating to django-admin.py:
It puts your project’s package on sys.path.
It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.
It calls django.setup() to initialize various internals of Django.
My question is : how can I manually set up these variables ? Because in my case I downloaded all the files on an arbitrary directory, but it was not enough. Eveything is here, but it is missing the link to this everything....
If you want to manually set the address of your config file, you can set DJANGO_SETTINGS_MODULE with the following:
export DJANGO_SETTINGS_MODULE='settings_to_load'
You can set all your environmental variables this way and it should work. I recommend to use a Virtualenv to this at least.

Configuring postgresql database for local development in Django while using Heroku

I know there are a lot of questions floating around there relating to similar issues, but I think I have a specific flavor which hasn't been addressed yet. I'm attempting to create my local postgresql database so that I can do local development in addition to pushing to Heroku.
I have found basic answers on how to do this, for example (which I think is a wee bit outdated):
'#DATABASES = {'default': dj_database_url.config(default='postgres://fooname:barpass#localhost/dbname')}'
This solves the "ENGINE" is not configured error. However, when I run 'python manage.py syncdb' I get the following error:
'OperationalError: FATAL: password authentication failed for user "foo"
FATAL: password authentication failed for user "foo"'
This happens for all conceivable combinations of username/pass. So my ubuntu username/pass, my heroku username/pass, etc. Also this happens if I just try to take out the Heroku component and build it locally as if I was using postgresql while following the tutorial. Since I don't have a database yet, what the heck do those username/pass values refer to? Is the problem exactly that, that I need to create a database first? If so how?
As a side note I know I could get the db from heroku using the process outlined here: Should I have my Postgres directory right next to my project folder? If so, how?
But assuming I were to do so, where would the new db live, how would django know how to access it, and would I have the same user/pass problems?
Thanks a bunch.
Assuming you have postgres installed, connect via pgadmin or psql and create a new user. Then create a new database and with your new user as the owner. Make sure you can connect via psql with the new user into to the database. you will then need to set up an env variable in your postactivate file in your virtualenv's bin folder and save it. Here is what I have for the database:
export DATABASE_URL='postgres://{{username}}:{{password}}#localhost:5432/{{database}}'
Just a note: adding this value to your postactivate doesn't do anything. The file is not run upon saving. You will either need to run this at the $ prompt, or simply deactivate and active your virtualenv.
Your settings.py should read from this env var:
DATABASES = {'default': dj_database_url.config()}
You will then configure Heroku with their CLI tool to use your production database when deployed. Something like:
heroku config:set DATABASE_URL={{production value here}}
(if you don't have Heroku's CLI tool installed, you need to do it)
If you need to figure how exactly what that value you need for your production database, you can get it by logging into heroku's postgresql subdomain (at the time this is being written, it's https://postgres.heroku.com/) and selecting the db from the list and looking at the "Connection Settings : URL" value.
This way your same settings.py value will work for both local and production and you keep your usernames/passwords out of version control. They are just env config values.

manage.py not updating database name

I have a Django application, and I recently changed the name of the database it is supposed to use. However, manage.py doesn't seem to be using the new database.
I've doublechecked the settings.py file, and I've even added a "print settings.DATABASE_NAME" to the manage.py file, and it prints out the correct name, but still connects to the old database.
For example, using ./manage.py dbshell:
NewDB
Password for user :
Welcome to psql 8.1.11, the PostgreSQL interactive terminal.
OldDB=>
So as far as I can see, it's completely ignoring what's in the settings file.
What could be causing this?
I figured out what the problem was.
While manage.py does take the settings file, the djang.conf.settings is loaded from the sys.path rather than what is passed via manage.py.
Apparently, the old location was in the sys.path, and so it was loading the old settings file.