Getting Django and PostgreSQL to work - django

I am trying to get Django and PostgreSQL to work.
So far I am getting the following error when I run syncdb.
....
django.core.exceptions.ImproperlyConfigured:
Error loading psycopg2 module: No module named psycopg2
The following is my settings.py.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'mydb', # Or path to database file if using sqlite3.
'USER': 'username', # Not used with sqlite3.
'PASSWORD': 'pwd123', # Not used with sqlite3.
'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '5432', # Set to empty string for default. Not used with sqlite3.
}
}
I think I have installed psycopg2 correctly, but I am not sure (through MacPorts).
Is there a way whether I can check whether psycopg2 is installed?
This link contains the install log of psycopg2
UPDATE
I got it working with the method below, but how do I check whether psycopg2 was actually installed before? and if so how to remove it completely?

If you are using MAC, make sure psycopg2 is installed and accesible to your main python interpreter.
This is how I'd install it on a mac:
$ sudo easy_install django
$ sudo easy_install psycopg2
Then test it:
$ python
>>> import django
>>> import psycopg2
You should not get any errors.
Also, if you are using an Eclipse/PYDEV, make sure you reconfigure your interpreter after installing django and psycopg2 libraries.

Related

django-admin dbshell raises django.core.exceptions.ImproperlyConfigured

I know this question has been asked before, but none of them worked for me so far, so I'm going to give it a chance here.
I'm trying to use MySQL as my database in django, but when I modify the settings.py and run the command:
django-admin dbshell
I get the following error:
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES,
but settings are not configured. You must either define the environment
variable DJANGO_SETTINGS_MODULE or call settings.configure()
before accessing settings.
What I did:
- I'm running windows 10.
using pipenv, I create fresh virtual environment.
install django.
start new project.
edit the settings.py
in the settings.py I change the DATABASES to the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test_db',
'USER': 'root',
'PASSWORD': '****',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
also pip installed mysqlclient
Weird thing is that when I make migrations, new tables are created, which means that DB works. why doesn't the command
django-admin dbshell
work?
based on django docs, manage.py does same thing as django-admin, and also adds settings to the sys path, therefore manage.py should be used instead of django-admin
https://docs.djangoproject.com/en/2.1/ref/django-admin/

Django to work with south requires MySQLdb

I am following the django instruction to learn django in eclipse.
I came to the part of running cmd
python manage.py migrate
and it complains about unknown command migrate.
Googled. Knew that it requires South module to be included. I downloaded/installed south, and added 'south' in the INSTALLED_APPS.
I ran the command again, this time it complains
import MySQLdb as Database
ImportError: No module named 'MySQLdb'
So I looked for MySQLdb, only to find that there is none for python 3.
I could not find anything useful. So what do you do to make django to work with mysql?
I know there're other connectors around, but I am trying to follow the django tutorial and it seems that 'migrate' cmd must use 'south' and 'south' must use MySQLdb(?)
--- update ---
Here is the DB settings in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'root',
'PASSWORD': '******',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
I suspect 'ENGINE' has to be something else, but I failed to find enough information online to figure it out...
You can switch to any database you want MySQL or postgresql or sqlite etc for your django app. South uses the default database engine from your django setting DATABASES. As stated here
South automatically exposes the correct set of database API operations
as south.db.db; it detects which database backend you’re using from
your Django settings file.

settings.DATABASES is improperly configured. Please supply the ENGINE value

I am trying to write a script that reads a table and inserts into other using Django's orm'.
the code can be found here:
http://pastebin.com/CfUtqve6
problem is when i try to run on terminal i get this error
"django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details."
my settings is rightly configured as ./manage.py runserver makes no complaints.
for clarity its like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'zingyhomes', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'root', # Not used with sqlite3.
'HOST': '127.0.0.1', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
whats causing this trouble, how should i overcome it and write that script thats eluding me so far ;)
Your problem is that you are not referencing your actual settings file in any way. You import the default Django settings, and then call settings.configure(): but of course they don't include your DATABASES setting.
As Iain points out in the comments, you should write this script as a custom management command, as that will take care of the settings stuff for you.

settings.DATABASES is improperly configured

total newbie here, sorry
Mac OSX 10.8 Python 2.7 (installed with homebrew)
PostgreSQL 9.4(installed with homebrew)
psycopg2 2.5 (installed with macports)
Django 1.0.4 (installed via python setup.py install)
I'm using this tutorial, and after starting python manage.py shell I ran
>>> from django.db import connection
>>> cursor = connection.cursor()
and got the following:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-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.
The DATABASES section of my settings.py file looks like this:
DATABASE_ENGINE = 'django.db.backends.postgresql_psycopg2' #postgresql_psycopg2
DATABASE_NAME = 'mydatabase' #mydatabase
DATABASE_USER = 'sarahr6' # Not used with sqlite3.
DATABASE_PASSWORD = '' # Not used with sqlite3.
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
So I can't figure out why it says it's improperly configured?
You need to specify DATABASES dictionary in settings.py:
A dictionary containing the settings for all databases to be used with
Django. It is a nested dictionary whose contents maps database aliases
to a dictionary containing the options for an individual database.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydatabase',
'USER': 'sarahr6',
'PASSWORD': '',
'HOST': '',
'PORT': ''
}
}

South needs DROP privileges, that seems unsafe, is there a way of working to avoid this

I introduced South in a project. I got into trouble, because of the fact that South needs DROP privileges. The error I got:
(1142, "DROP command denied to user '?????' for table 'ROLLBACK_TEST'")
After that, the migration seemed borked. I had to drop my database and re-create it.
When running the South command, the 'default' Django connection is used. So, the associated user (that is also used for the front-end website) needs to have DROP privileges. To me, this seems a bit dangerous. Is there a way around this potentially unsafe approach?
1) Copy your settings.py to a new file south_settings.py, and remove everything except the DATABASES key.
2) Prepend from settings import * to import the existing settings.py keys.
3) Edit the USER and PASSWORD values to reflect a newly created SQL user with DROP privileges.
# south_settings.py
from settings import *
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': 'username_with_drop_privileges', # Not used with sqlite3.
'PASSWORD': 'password_with_drop_privileges', # 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.
}
}
You merely override the DATABASES key value in this new south_settings.py.
4) Finally, execute python manage.py [command] using the --settings=south_settings argument.
python manage.py migrate [app] --settings=south_settings