django mysql gives page not found (404) while sqlite works fine - python

I have two settings files for my django project with different database settings.
First:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbname',
'USER': 'user',
'PASSWORD': 'megahardbreakingpassword',
'HOST': 'localhost',
'PORT': '',
'OPTIONS': {
'init_command': 'SET storage_engine=INNODB',
}
}
}
And second:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'db.sqlite3',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
When I run server with second database settings project works fine, but when I run first settings file with mysql, django returns 404 error for all urls.
I also use django-hosts for implementing subdomains patterns, so maybe something wrong here. My hosts.py code:
from django_hosts import patterns, host
host_patterns = patterns('',
host(r'^$', 'project.urls', name='host'),
)
I use django 1.4.2 and Python 2.7.9

And finally I found answer! According to e4c5's comment I searched problem in my views instead of my settings files. I wrote simple unittest and it gives me some strange tracebeck about my MySQL database such as wrong column name and deprecation warnings about duplication indexes.
I reinstall older version of MySQL server (5.0.70 instead of 5.6.25), run syncdb and migrations again and it completely solved my problems. So God bless TDD!

Related

Can't reference Django database with dj_database_url

I have a Django application using decouple and dj_database_url (it is not on Heroku). If I put my connection information in settings.ini (DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, and DB_PORT) and set up my database connection in settings.py like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': config('DB_NAME'),
'USER': config('DB_USER'),
'PASSWORD': config('DB_PASSWORD'),
'HOST': config('DB_HOST'),
'PORT': 'DB_PORT',
}
}
It works fine.
However, if I put this in my settings.ini:
DATABASE_URL=postgres://johndoe:mypassword#123.456.789.000:5000/blog_db
and reference it like this in my settings.py:
DATABASES = {"default": dj_database_url.config(default=config("DATABASE_URL"))}
It doesn't work. I just get a 500 when I try to run my server. I presume there is something wrong with my syntax. Can anyone point out what I'm doing wrong?
Thanks!

Running two databases on heroku with django

I have two databases that my Django application needs access. One is a shared database owned by a separate app with the Django app only having read access. The second is entirely owned by the Django app.
For local development I am ok but I'm not sure how to configure things so that Heroku uses the second database.
Currently I have the shared database promoted to DATABASE_URL and the secondary database is at HEROKU_POSTGRESQL_BLUE_URL.
In my settings I have:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'main_database_name',
'USER': 'username',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}, 'secondary': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'secondary_database_name',
'USER': 'username',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
Please ask any more questions if you need me to clarify.
Thanks!
In summary, my specific problem is: I don't know how to have Heroku use the HEROKU_POSTGRESQL_BLUE_URL as the "secondary" database.
---Edit----
At the bottom of settings.py:
# Configure Django App for Heroku.
import django_heroku
django_heroku.settings(locals())
This is where the connection is made between my app's default database and Heroku's DATABASE_URL. I still haven't solved the issue but after some troubleshooting help in the comments, I believe the answer will be found in there.
Here is my working solution.
I have a local_settings.py file not tracked by version control. This contains the database settings for the developer's postgres instance.
local_settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'main_database_name',
'USER': 'username',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}, 'secondary': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'secondary_database_name',
'USER': 'username',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
Then in settings.py I try to import the local_settings.py file. If it doesn't exist, I use the django_heroku package to configure everything for Heroku. In my case, I need to pass the keyword argument databases=False since I want to do a more custom configuration for the databases.
settings.py (only the relevant parts)
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
# These database settings are used for heroku deployments. They are overwritten with local_settings.py in development.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('DATABASE_URL'),
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}, 'secondary': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('SECONDARY_DATABASE_URL'),
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
try:
from local_settings import *
except ImportError as e:
# Configure Django App for Heroku.
import django_heroku
django_heroku.settings(locals(), databases=False)
In order for this to work you would need to set the Heroku env variable SECONDARY_DATABASE_NAME to the url of your database.
To find out the URL of your secondary database run heroku config -a your_app_name. Copy the url and then set a new environment param in heroku by running
heroku config:set SECONDARY_DATABASE_URL=postgres://xxxxxxx -a your_app_Name
There are a lot of choices with how you would handle the ENV variables, I liked this because I can just set it in my staging and production environments and the same settings work for both.

Django migrate tables to new database

I originally had a django project with a single app and all models were defined in that app. The project, when initiated only used the default database. It has now become an unwieldy app that I'm trying to break down into smaller apps. Doing so, I want to use different databases for the different apps. I've setup new databases and a router in the settings.py file. However, I'm confused about how to migrate existing tables to the new databases.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER': 'db_user_name',
'PASSWORD': 'password',
'HOST': 'hostname',
'PORT': '3306',
},
'db2': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name2',
'USER': 'db_user_name2',
'PASSWORD': 'password2',
'HOST': 'hostname2',
'PORT': '3306',
}
}
I would want an app (e.g. app1) to be moved from default to db2. The router already knows to specify app1 to db2 but running migrations is doing nothing. Any ideas?
#knbk's answer was ultimately correct, except that the solution involved an additional step.
1. python manage.py migrate auth --database=db2
2. python manage.py migrate app1 --database=db2

Django configuration: settings.DATABASES is improperly configured

I started a Django project, but I get an error with database settings. The homepage works fine:
It worked!
Congratulations on your first Django-powered page.
Of course, you haven't actually done any work yet. Next, start your first app by
running python manage.py startapp [appname].
You're seeing this message because you have DEBUG = True in your Django settings file
and you haven't configured any URLs. Get to work!
But I get this error is console:
settings.DATABASES is improperly configured. Please supply the ENGINE
value. Check settings documentation for more details.
Request Method: GET Request URL: http://fireidea.net/ Django
Version: 1.6.2 Exception Type: ImproperlyConfigured Exception Value:
settings.DATABASES is improperly configured. Please supply the ENGINE
value. Check settings documentation for more details.
Exception Location:
/home2/minhhien/webapps/django/django/db/backends/dummy/base.py in
complain, line 15 Python Executable: /usr/bin/python Python Version:
2.7.5
...
Database settings:
MANAGERS = ADMINS
DATABASE_ENGINE = 'mysql'
DATABASE_NAME = ''
DATABASE_USER = ''
DATABASE_PASSWORD = ''
DATABASE_HOST = 'localhost'
DATABASE_PORT = ''
#DATABASES = {
# 'default': {
# 'ENGINE': '',
# 'NAME': '',
# 'HOST': 'localhost',
# 'PORT': '',
# 'USER': '',
# 'PASSWORD': ''
# }
#}
Django 1.6 expects DATABASES to be a dictionary.
You commented it out, and put some older settings format in there.
You should change it back to:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '',
'HOST': 'localhost',
'PORT': '',
'USER': '',
'PASSWORD': ''
}
}
See more about this here: https://docs.djangoproject.com/en/1.6/ref/databases/
You're using settings variables DATABASES_* which was already deprecated in Django 1.2, 5 years ago now!
You need to use a dictionary, as the default setting might have pre-filled for you:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '',
'HOST': 'localhost',
'PORT': '',
'USER': '<user>',
'PASSWORD': ''
}
}

Configure Django with MS SQL Server database

I'm setting up a Django application and I want to use SQL Server 2012 for my database.
To configure my website I'm following this section of the official Django documentation.
In the section Database setup I found instructions for changing RDBMS.
And in settings.py file I found these instructions for setting up Django with SQlite.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
How I can change this configuration to use SQL Server 2012 instead?
Looks like you'll need django-mssql which requires django 1.4 or below:
http://django-mssql.readthedocs.org/en/latest/
It has a pip package: https://pypi.python.org/pypi/django-mssql
Then include in your settings.py:
DATABASES = {
'default': {
'NAME': 'my_database',
'ENGINE': 'sqlserver_ado',
'HOST': 'dbserver\\ss2008',
'USER': '',
'PASSWORD': '',
}
}

Categories

Resources