摘要:Template
查詢資料表並Show在網頁上
views.py
#views.py def Test(request): assert isinstance(request,HttpRequest) db=Connect('127.0.0.1','root','','test') #資料庫連線 cursor= db.cursor() cursor.execute('select * from new_table') #查詢 abc=cursor.fetchall() #取得所有資料 db.close() #關閉連線 return render(request,'app/test.html',RequestContext(request,{ 'mydata':abc}))
urls.py
url(r'^Test$', 'app.views.Test', name='Test'),
test.html
<ul>
{% for item in mydata %}
<li><p>Name:{{item.1}}</p><p>Bir:{{item.3|date:"Y/m/d"}}</p></li>
{% endfor %}
</ul>
結果:
-
Name:Tom
Bir:2015/07/16
-
Name:Jack
Bir:2015/07/15
-
Name:Bill
Bir:2015/08/10