一、Install module 'mysqlclient'
in "testvir2" virtual environment
pip install mysqlclient
二、Settings 配置
1. DATABASE
"settings.py" ->
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'testdjango',
'USER': "root",
'PASSWORD': "password",
'HOST': "192.168.251.88",
}
}
2. TEMPLATES下的DIRS
"djangoStart/Settings.py" ->
change:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
'DIRS': [os.path.join(BASE_DIR, 'templates')]
3. 新建STATICFILES_DIRS
"djangoStart/Settings.py" ->
add:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
"static"是新建存放静态文件的文件夹名
三、migration生成数据表
1. Tools -> Run manage.py Task...
2. in 'manage.py@djangoStart'
'makemigrations'
'migrate'
django 已经在数据库'testdjango'里面生成需要用到的表
四、编写views.py
1 . "apps/message/views.py" ->
add:
def getform(request):
return render(request, 'message_form.html')
五、配置urls.py
1. "djangoStart/urls.py" ->
add:
from message.views import getform
change:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^form/$', getform),
]
六、配置"form.html"和"css"文件
1. html和css文件分离
a. copy "form.html" to "templates"
b. change "form.html" to "message_form.html"
2. css文件分离与地址修改
b. create directory "css" in "static"
c. create "style.css" in "css"
d. copy css into "style.css"
- change "<link rel="stylesheet" href="/static/css/style.css">" in "message_form.html"