Crossplatform deployment - django

I'm working with Django project on Linux and Windows. So I'm trying to config 'settings.py' to work on both platforms.
I can config the template dirs as:
TEMPLATE_DIRS = (
'c:/artefacts/workspace/BookMixToFb2/src/templates',
'/home/demas/workspace/BookMixToFb2/src/templates'
)
and this will be work on linux and windows.
But when I setup a database connection I have only one possibility to set way to the database file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
#'NAME': 'C:\\Users\\ademidov.GMCS\\workspace\\BookMixToFb2\\sqlite.db', # Or path to database file if using sqlite3.
'NAME': '/home/demas/workspace/BookMixToFb2/sqlite.db', # Or path to database file if using sqlite3.
Is there any way to set two different paths to database file and pass the current path as command line argument?

Yes, you should use this tip: Use os.path.dirname() in settings.py to avoid hardcoded dirnames.

Related

Using fakeredis in a Django development settings file?

My Django settings.py file contains the following configuration options:
# Caches
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.redis.RedisCache',
'LOCATION': 'redis://redis:6379',
}
}
# Queues
RQ_QUEUES = {
'default': {
'HOST': 'redis',
'PORT': 6379,
'DB': 0,
'DEFAULT_TIMEOUT': 360,
},
}
Both CACHES and RQ_QUEUES contain configuration details that point to a redis server.
Is it possible to reconfigure these settings to point to an instance of fakeredis instead ?
I have reviewed the fakeredis documentation and so far I have only seen examples where the redis connection is manually over-ridden, every time a call to redis is made.
It seems to me that when running tests, it would be much more convenient to simply point the Django CACHE location directly to fakeredis.
Is this possible?

Same Django projects running on different databases

I am trying to run two identical Django projects on different databases, one for production using a certain port number (say, 80) and the other for testing using another port number (say, 8000). I also used Nginx and Gunicorn as the reverse proxy server and the application server, with Nginx listening to ports 80 and 8000 and forwarding to gunicorn of ports 8001 and 8002, respectively.
The problem is: how do I know the port number of the request in Django's settings.py so that the project can choose different databases?
The standard practice for doing this in django is to create a local_settings.py file
Put this at the top of the local_settings.py file:
try:
from settings import *
except ImportError:
pass
Now in local_settings.py you must override the following variable:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'dbuser',
'PASSWORD': 'dbpassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
With different values for both projects
Then when running Django you need to set the following environment variable:
export DJANGO_SETTINGS_MODULE="appname.local_settings"
One way to bunch this all together is to create a run.sh file that first sets this variable, then runs gunicorn
To sum it up, settings.py is common between both projects, local_settings.py overrides those variables that are different between different projects

Error loading MySQLdb module: No module named MySQLdb

i am getting this error when running python manage.py syncdb.
I've tried installing all the packages that were described in various posts to no avail.I desperately want a solution as i have lost too many time in this!
Thanks in advance for any help!
Edit: Also my setting for database is:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg3', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'db1', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'XYZ', # Not used with sqlite3.
'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '3306', # Set to empty string for default. Not used with sqlite3.
}
}
My O.S is Linux Mint 17.1 Mate.
Also do i need to install django in a virtual environment?
Try installing this way
apt-get install python-dev libmysqlclient-dev
pip install MySQL-python
Phew!
Got it working at last!
Seems like my Django package was not compatible with libs & dependencies.I created a virtual environment and installed the latest Django package as well as its dependencies, libs etc.
That is it!

How do I get Jython-Django to work with Postgresql JDBC

I have tried to setup an instance of Django on Jython using Postgresql. I can't get Django to talk to Postgresql. Can someone help me figure out what is wrong?**
So far, I have the following system installed for development:
Windows 7
JDK7 (x86)
Jython Version 2.7b2
Django-1.6.5
django-jython 1.7.0b2(Database backends and management commands, for development underDjango/Jython)
postgresql-9.3.5-1-windows-x64.exe
Eclipse-PyDev
I was able to run the server and database, but now I am having issues with configuring Django to work with the Postgresql database.
I followed the configuration steps here:
https://pythonhosted.org/django-jython/database-backends.html#postgresql
My settings.py file has the following:
DATABASES = {
'default': {
'ENGINE': 'doj.db.backends.postgresql',
'NAME': 'gate',
'USER': 'gate',
'PASSWORD': 'test',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
When I check my class path:
C:\>echo %CLASSPATH%
C:\Program Files (x86)\Java\jdk1.7.0_67\bin.;C:\Program Files (x86)\Java\jre7\li
b\ext\QTJava.zip
I have the JDBC file postgresql-9.3-1102.jdbc41.jar in the lib folder listed above.
Error Message from Django
File "C:\jython2.7b2\Lib\site-packages\django\db\utils.py", line 131, in load_backend raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: 'doj.db.backends.postgresql' isn't an available
database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
u'mysql', u'oracle', u'postgresql_psycopg2', u'sqlite3'
Error was: No module named schema
I believe the problem is that you are using the wrong version of Django.
Jython-Django is compatible only starting from 1.7.
See https://docs.djangoproject.com/en/1.6/howto/jython/

Error was: No module named backends.spatialite.base

For testing purposes I'm trying to use SQLite, but it seems I'm missing the spatialite backend:
django.core.exceptions.ImproperlyConfigured: 'django.contrib.gis.backends.spatialite' isn't an available database backend.
Try using django.db.backends.XXX, where XXX is one of:
'dummy', 'mysql', 'oracle', 'postgresql_psycopg2', 'sqlite3'
Error was: No module named backends.spatialite.base
Any idea how I can make Django find it?
I have installed spatialite.
This is my db setup:
DATABASES['default'] = {
'ENGINE': 'django.contrib.gis.backends.spatialite',
'NAME': '/tmp/test.db'
}
Ubuntu 12.04.1 LTS; Python 2.7.3; Django 1.4.1
You should look for:
django.contrib.gis.db.backends.spatialite
not
django.contrib.gis.backends.spatialite
:)
trying importing from shell