摘要:MySql 連線設定
settings.py
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'test', 'USER': 'root', 'PASSWORD': '', 'HOST': '127.0.0.1', 'PORT': '3306', } }
views.py
from django.db import connection def Test(request): assert isinstance(request,HttpRequest) cursor= connection.cursor() cursor.execute('select * from new_table') abc=cursor.fetchall() cursor.close() return render(request,'app/test.html',RequestContext(request,{ 'mydata':abc}))
補充:urls.py , test.html 請參考 [Django] Template