0

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  |
+----+-----------+-----------+------+
2
  • 2
    Why are you directly using MySQL statements rather than the ORM? Commented Mar 13, 2014 at 15:06
  • Ian Clark, how do i play around it, since these are the heavily use and queried about to 10000 hosts. Commented Mar 14, 2014 at 4:28

3 Answers 3

1

Thanks all for you timely help..

@eddwinpaz, it got fixed in views

== views.py:

def gethost(request, hostname, template_file="gethost.html"):
  from django.db import connection, transaction
  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)
Sign up to request clarification or add additional context in comments.

Comments

0

Your url is not passing anything and recomended to use ORM instead of SQL based.

url(r'^gethost/(?P<hostname>[\w\-]+)/$', 'dc.views.gethost'),

4 Comments

thanks, in this case how do I call url to form/build a query?
should be www.domain.com/gethost/anothersite.com
Yeah, tried one, that does not gives me output to read db... @eddwinpaz, it might be the issue at views
thanks for your help, did fix this in views also your urls help to get the hostname.
0

Look at this code below hope it makes more clear of what you want to achieve. make sure on your settings.py there is the URL for the TEMPLATE_DIRS = ('var/www/dc/templates/') in your case use the path on your system on settings.py. also make sure on INSTALLED_APPS is 'dc' added.

settings.py

TEMPLATE_DIRS = ('var/www/dc/templates/')

views.py

from django.shortcuts import render, HttpResponseRedirect, render_to_response
from dc.models import inventory

def gethost(request, hostname):

   try:
       inventory_data = inventory.objets.get(hostname=hostname)
       return render(request,'gethost.html',{inventory:'inventory_data'})

   except inventory.DoesNotExist:

         return HttpResponseRedirect('/myurl/') 
         # this is a dummy url you must set it to a 404 if you want

gethost.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h2>View Hostname: {{inventory.hostname}}</h2>

</body>
</html>

models.py

from django.db import models

class inventory(models.Model):

hostname = models.CharField(max_length=100)

def __unicode__(self):
    return self.hostname

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.