导航

 

我用的mysql版,先上无模板的版本,在views里加上

import MySQLdb

然后在下方写函数

def use_databases(request):
    db = MySQLdb.connect(user='root', db='main', passwd='', host='localhost')
    cursor = db.cursor()
    cursor.execute('select * from  start_start')
    now = [row[0] for row in cursor.fetchall()]
    html   = "<html><body>It is now %s.</body></html>"   % {{now}}
    db.close()
    return HttpResponse(html)

然后去urls文件里加上use_databases的路径配置

url(r'^mytimes/$', use_databases),

然后是有模板版,模板需要在templates文件夹下添加一个模板html文件,如下:

<!DOCTYPE html>
<html><body>It is now {{ current_date   }}.</body></html>

该文件名为current_datetime.html

然后views里导入包

from   django.template import Template, Context

再添加方法如下

def use_database(request):
    db = MySQLdb.connect(user='root', db='main', passwd='', host='localhost')
    cursor = db.cursor()
    cursor.execute('select * from  start_start')
    now = [row[2] for row in cursor.fetchall()]
    db.close()
    return render_to_response('current_datetime.html', {'current_date': now})

再次重启服务器后就可以看到相应结果



sqllite版请见http://www.cnblogs.com/haozi0804/p/4489063.html

posted on 2016-01-24 15:38  slqt  阅读(209)  评论(0编辑  收藏  举报