### templates中test.html
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="/static/css/test.css">
</head>
<body>
<form action="{% url 'go_test' %}" method="post" class="smart-green">
<h1>留言信息
<span>请留下你的信息.</span>
</h1>
<label>
<span>姓名 :</span>
<input id="name" type="text" name="name" class="error" value="{{ message.name }}" placeholder="请输入您的姓名"/>
<div class="error-msg"></div>
</label>
<label>
<span>邮箱 :</span>
<input id="email" type="email" name="email" value="{{ message.email }}" placeholder="请输入邮箱地址"/>
<div class="error-msg"></div>
</label>
<label>
<span>联系地址 :</span>
<input id="address" type="text" name="address" value="{{ message.address }}" placeholder="请输入联系地址"/>
<div class="error-msg"></div>
</label>
<label>
<span>留言 :</span>
<textarea id="message" name="message" placeholder="请输入你的建议">{{ message.message }}</textarea>
<div class="error-msg"></div>
</label>
<div class="success-msg"></div>
<label>
<span> </span>
<input type="submit" class="button" value="提交"/>
</label>
{% csrf_token %}
</form>
</body>
</html>
```
### static/css/test.css文件
```
.smart-green {
margin-left: auto;
margin-right: auto;
max-width: 500px;
background: #F8F8F8;
padding: 30px 30px 20px 30px;
font: 12px Arial, Helvetica, sans-serif;
color: #666;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
}
.smart-green h1 {
font: 24px "Trebuchet MS", Arial, Helvetica, sans-serif;
padding: 20px 0px 20px 40px;
display: block;
margin: -30px -30px 10px -30px;
color: #FFF;
background: #9DC45F;
text-shadow: 1px 1px 1px #949494;
border-radius: 5px 5px 0px 0px;
-webkit-border-radius: 5px 5px 0px 0px;
-moz-border-radius: 5px 5px 0px 0px;
border-bottom: 1px solid #89AF4C;
}
.smart-green h1 > span {
display: block;
font-size: 11px;
color: #FFF;
}
.smart-green label {
display: block;
margin: 0px 0px 5px;
}
.smart-green label > span {
float: left;
margin-top: 10px;
color: #5E5E5E;
}
.smart-green input[type="text"], .smart-green input[type="email"], .smart-green textarea, .smart-green select {
color: #555;
height: 30px;
line-height: 15px;
width: 100%;
padding: 0px 0px 0px 10px;
margin-top: 2px;
border: 1px solid #E5E5E5;
background: #FBFBFB;
outline: 0;
-webkit-box-shadow: inset 1px 1px 2px rgba(238, 238, 238, 0.2);
box-shadow: inset 1px 1px 2px rgba(238, 238, 238, 0.2);
font: normal 14px/14px Arial, Helvetica, sans-serif;
}
.smart-green textarea {
height: 100px;
padding-top: 10px;
}
.smart-green .button {
background-color: #9DC45F;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-border-radius: 5px;
border: none;
padding: 10px 25px 10px 25px;
color: #FFF;
text-shadow: 1px 1px 1px #949494;
}
.smart-green .button:hover {
background-color: #80A24A;
}
.error-msg{
color: red;
margin-top: 10px;
}
.success-msg{
color: #80A24A;
margin-top: 10px;
margin-bottom: 10px;
}
```
### 生成操作数据库的UserMessage和数据表
编写models.py
```
# _*_ coding:utf-8 _*_
from django.db import models
# Create your models here.
class UserMessage(models.Model):
#object_id = models.ChatField(max_length=50, default="", primary_key = True, verbose_name=u"主键")
name = models.CharField(max_length=20, default="", verbose_name=u"用户名")
email = models.EmailField(verbose_name=u"邮箱")
address = models.CharField(max_length=100, verbose_name=u"联系地址")
message = models.CharField(max_length=500, verbose_name=u"留言信息")
class Meta:
verbose_name = u"用户留言信息"
# db_table="user_message" 指定固定表名,不指定则为模块名_类名小写(UserMessage的小写)
# ordering = "-id" 做排序
```
![](https://box.kancloud.cn/fbeea14d5433aef81333198395730cf1_579x123.png)
![](https://box.kancloud.cn/4acd99e7ae61d695ee2087c013955a55_399x138.png)
### 在message模块中views.py做增删改查的基本操作
```
# _*_ coding:utf-8 _*_
from django.shortcuts import render
# Create your views here.
from .models import UserMessage
def getTest(request):
# all_messages = UserMessage.objects.all()
# all_messages = UserMessage.objects.filter(name='test',address='上海')
# for message in all_messages:
# print(message.name)
#修改
message = None
all_messages = UserMessage.objects.filter(name='test')
if all_messages:
message = all_messages[0]
#删除
# all_messages = UserMessage.objects.filter(name='test',address='上海')
# all_message.delete()
#增加
#user_message = UserMessage()
# user_message.name = 'test2'
# user_message.address = '上海'
# user_message.email = 'admin@123.com'
# user_message.message = '上海迪士尼'
# user_message.save()
if request.method == "POST":
name = request.POST.get('name','')
email = request.POST.get('email','')
address = request.POST.get('address','')
message = request.POST.get('message','')
user_message = UserMessage()
user_message.name = name
user_message.address = address
user_message.email = email
user_message.message = message
user_message.save()
return render(request, 'test.html', {'message': message})
```
### 配置路由urls.py
```
from django.contrib import admin
from django.urls import path
from message.views import getTest
urlpatterns = [
path('admin/', admin.site.urls),
path('test/', getTest, name='go_test')
]
```