在Django中可以在视图函数中使用python来实现,不过更简单的方法是在settings.py中设置,这样可以减少冗余代码,实现数据层剥离。
相信大多数人使用的是Mysql,这里不介绍Mysql怎么装以及配置了,打开settings.py设置如下:
DATABASES = {
'default': {
#'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'ENGINE':'django.db.backends.mysql',
'NAME': 'test', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'wangxb', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}