Django. how to get a host name for db
I'm trying to connect to database: in localhost I can do something like this:
db = MySQLdb.connect(host='127.0.0.1', user="root", passwd="", db="schooldb")
but in the server I want '127.0.0.1' to be a variable, unfortunately
h = request.get_host() return HttpResponse(h)
gives me 127.0.0.1:8000 So my question is how to truncate port and leave only 127.0.0.1. Or may be you can suggest something better.
[request.get_host()] gives you the server information for the request and not the database server information.
To get database information (assuming you have only one database named default), you can do:
>>> from django.conf import settings >>> settings.DATABASE['default']['HOST'] >>> 'MyDBServer' >>> settings.DATABASE['default']['PORT'] >>> '9999'
Do you want to have different environments for local and server purpose?
If yes and you are using django I think that the easiest way is to use django settings system and setup database hostname there.
On the server you can have other file with different settings and start server with that module. For example using manage.py tool with --settings flag.
./manage.py runserver --settings=project.settings_production
Edit: Remeber that development server is not for use on production environment.
By the way, request.get_host() returns a remote hoste name.