ตั้งค่าฐานข้อมูล MySQL กับ Django
โมดูลที่ต้องการต้องรองรับ Django DB backend ครับ โมดูลที่รองรับคือ mysql-connector-python ครับ dev.mysql.com/doc/connector-python/en/index.html เมื่อทำการติดตั้งแล้วให้เข้าไปแก้ไขที่ไฟล์ settings.py ครับDATABASES = {
'default': {
'NAME': 'user_data', #ชื่อฐานข้อมูล
'ENGINE': 'mysql.connector.django',
'USER': 'mysql_user', #ชื่อผู้ใช้
'PASSWORD': 'mysql_', #รหัสผ่าน
'HOST': 'dbhost', #โฮมฐานข้อมูล
'PORT': '3306' #port ของโฮมฐานข้อมูล
}
}
แล้วบันทึกไฟล์ครับ
ตั้งค่าฐานข้อมูล PostgreSQL กับ Django
ให้ติดตั้งโมดูล psycopg2 ครับ เมื่อต้องการเสร็จแล้วให้เข้าไปแก้ไขที่ไฟล์ settings.py ครับDATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db_name', #ชื่อฐานข้อมูล
'USER': 'db_user', #ชื่อผู้ใช้
'PASSWORD': 'db_user_password', #รหัสผ่าน
'HOST': '' #โฮมฐานข้อมูล
}
}
ตั้งค่าฐานข้อมูล Oracle กับ Django
ให้ผู้อ่านทำการติดตั้งโมดูล cx_Oracle โดยทำตามนี้ครับ python3.wannaphong.com/2014/08/Oracle-Python.html เมื่อติดตั้งเสร็จแล้วให้เข้าไปแก้ไขที่ไฟล์ settings.py ครับDATABASES = {
'default': {
'ENGINE': 'django.db.backends.oracle',
'NAME': 'db_name', #ชื่อฐานข้อมูล
'USER': 'db_user', #ชื่อผู้ใช้
'PASSWORD': 'db_user_password', #รหัสผ่าน
'HOST': '' #โฮมฐานข้อมูล
}
}
ตั้งค่าฐานข้อมูล SQLite3 กับ Django
ผู้อ่านไม่ต้องไปแก้ไขอะไรหลังติดตี่งตั้งแต่แรกครับ โดยในไฟล์ settings.py จะเป็นแบบนี้ครับDATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), #db.sqlite3 คือชื่อไฟล์ฐานข้อมูลsqlite3
}
}
เปิดการใช้งานหน้า admin ใน Django
เมื่อตั้งค่าฐานข้อมูลเสร็จแล้ว เราจะมาเปิดการใช้งานหน้า admin ใน Django กันครับ ให้ใช้คำสั่งpython manage.py syncdb
จะมีให้กรอก username, email, password ของ admin ครับ โดยอย่าลืมเลยนะครับ เสร็จแล้วให้เข้าไปที่ไฟล์ settings.py โดยเอา # ตรง #'django.contrib.admin', ใน INSTALLED_APPS ออกแล้วบันทึกไฟล์
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', )
แล้วเข้าไปที่ไฟล์ urls.py แล้วแก้ไขจาก
from django.conf.urls import patterns, include, url
from hello.views import myfunction
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
#url(r'^$', 'sample.views.home', name='home'),
# url(r'^sample/', include('sample.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
)
เป็น
from django.conf.urls import patterns, include, url
from hello.views import myfunction
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
#url(r'^$', 'sample.views.home', name='home'),
# url(r'^sample/', include('sample.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
บันทึกไฟล์แล้วรันหน้าเว็บโดยใช้คำสั่ง
python manage.py runserver
แล้วเข้าไปที่ http://127.0.0.1:8000/admin ครับ จะพบกับหน้าล็อกอิง Admin เมื่อล็อกอินเข้าไปแล้วจะพบกับหน้าดูแลของ Admin ครับ
ติดตามบทความต่อไปนะครับ
ขอบคุณครับ

0 ความคิดเห็น:
แสดงความคิดเห็น
แสดงความคิดเห็นได้ครับ :)