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.

Thank you

Answers


[request.get_host()][1] 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.


Need Your Help

How to use radius style in Flex plot charting

flex actionscript-3 charts

When I use radius property in Flex plot chart

Need a task tree view plugin for Redmine 1.1.2.stable

redmine redmine-plugins

Assume I have in Redmine several tasks with 'blocked by' or 'depends on' relations between them. When I look at result of 'my tasks' query Redmine shows all the tasks in a single list. Can you reco...