python 27+django呼び出しデータベース
1635 ワード
私が使っているmysql版は、まずテンプレートのないバージョンをつけてviewsに追加します.
次に関数を下に書きます
urlsファイルにuseを追加しますDatabasesのパス構成
次にテンプレート版があります.テンプレートはtemplatesフォルダの下にテンプレートhtmlファイルを追加する必要があります.以下のようにします.
ファイル名はcurrent_datetime.html
そしてviewsにパッケージをインポート
再追加方法は次のとおりです.
サーバーを再起動すると、対応する結果が表示されます.
sqllite版どうぞhttp://www.cnblogs.com/haozi0804/p/4489063.html
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