我是新來的django需要幫助,試圖用(Django == 1.4)構建庫存工具,這將很容易獲取列表主機/服務器從數據庫(MySQL)Django + MySQL,form /創建mysql查詢,將有效參數傳遞給url
我想要實現的是簡單地提供主機名作爲參數與URL並將其提取到Django應用程序,構建查詢並顯示在UI上的結果。
實例網址: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:
[[email protected] dc]# mysql dc -e 'SELECT * FROM inventory WHERE hosts="localhost"'
+----+-----------+-----------+------+
| id | groups | hosts | loc |
+----+-----------+-----------+------+
| 1 | localhost | localhost | sf |
+----+-----------+-----------+------+
爲什麼直接使用MySQL語句而不是ORM? –
伊恩克拉克,我怎麼玩它,因爲這些是大量使用和查詢約10000主機。 –