I am new to django need help, where trying to build inventory tool with (Django==1.4), which would be easily fetch the list of hosts/servers from database(MySQL)
What I am suppose to achieve is to simply provide the hostname as argument with url and fetch it into django application, build query and show the results on to UI.
Example URL: http://test.example.com/gethost/?hostname=localhost
== urls.py:
urlpatterns = patterns('',
# Examples:
url(r'^gethost', 'dc.views.gethost', name='gethost'),
== views.py:
def gethost(request, hostname, template_file="gethost.html"):
from django.db import connection, transaction
hostname = request.GET.get('hostname')
cursor = connection.cursor()
cursor.execute("SELECT * FROM inventory WHERE hosts='%s'" % 'hostname')
rows = cursor.fetchall()
t = Context({'results': rows})
return render_to_response(template_file, t)
mysql cmd:
[root@localhost dc]# mysql dc -e 'SELECT * FROM inventory WHERE hosts="localhost"'
+----+-----------+-----------+------+
| id | groups | hosts | loc |
+----+-----------+-----------+------+
| 1 | localhost | localhost | sf |
+----+-----------+-----------+------+