Django学习系列3:model的增删改查

编辑views.py:

from django.shortcuts import render
import MySQLdb

from .models import UserMessage

# Create your views here.

def getform(request):
all_messages = UserMessage.objects.all()
for message in all_messages:
print message.name
return render(request, 'message_form.html')

在mysql的表message_usermessage添加一条数据:


取出数据库内容:(查)

使用pycharm打一个断点:

打开浏览器刷新网页后,在pycharm下,按F6,选择for循环的变量message,点击后会有一行带“+号”的信息。

点击“+”号,即可查看断点执行情况,我们可以看到已经获取了mysql的表message_usermessage信息:

同时,console内也已经输出了我们print message.name的信息:

按F8退出断点查询操作。

按照条件取出数据:UserMessage.objects.filter方法

# _*_ coding: utf-8 _*_
from django.shortcuts import render
import MySQLdb

from .models import UserMessage

# Create your views here.

def getform(request):
#all_messages = UserMessage.objects.all()
all_messages = UserMessage.objects.filter(name = 'zone',address='北京')
for message in all_messages:
print message.name
return render(request, 'message_form.html')

向数据库中添加值:(增)

首先实例化一个user_message对象:user_message = UserMessage()

# _*_ coding: utf-8 _*_
from django.shortcuts import render
import MySQLdb

from .models import UserMessage

# Create your views here.

def getform(request):
#all_messages = UserMessage.objects.all()
# all_messages = UserMessage.objects.filter(name = 'zone',address='北京')
# for message in all_messages:
# print message.name
user_message = UserMessage()
user_message.name = "zone2"
user_message.message = "helloworld2"
user_message.address = "上海"
user_message.email = 2@2.com
user_message.object_id= "helloworld2"
user_message.save()

return render(request, 'message_form.html')

验证一下:启动系统,打一个断点,浏览器刷新一下页面:

单步调试,一直按F6并没有报错,打开数据库查看,已经添加了数据:

在web表单里显示字段并保存入数据库:

html页面:必须加上{% csrf_token %}


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="/static/css/style.css">
</head>
<body>
<form action="{% url 'go_from' %}" method="post" class="smart-green">
<h1>留言信息
<span>请留下你的信息.</span>
</h1>
<label>
<span>姓名 :</span>
<input id="name" type="text" name="name" class="error" placeholder="请输入您的姓名"/>
<div class="error-msg"></div>
</label>

<label>
<span>邮箱 :</span>
<input id="email" type="email" value="" name="email" placeholder="请输入邮箱地址"/>
<div class="error-msg"></div>
</label>

<label>
<span>联系地址 :</span>
<input id="address" type="text" value="" name="address" placeholder="请输入联系地址"/>
<div class="error-msg"></div>
</label>

<label>
<span>留言 :</span>
<textarea id="message" name="message" placeholder="请输入你的建议"></textarea>
<div class="error-msg"></div>
</label>
<div class="success-msg"></div>
<label>
<span>&nbsp;</span>
<input type="submit" class="button" value="提交"/>
</label>
{% csrf_token %}
</form>

</body>
</html>

在web页面填写相应的内容,提交后返回pycharm,调试可发现,已获取相应的值:(post方法通过name字段取值的)

编辑views.py:即完成了post方法与数据库插入的关联

# _*_ coding: utf-8 _*_
from django.shortcuts import render
import MySQLdb

from .models import UserMessage

# Create your views here.

def getform(request):
#all_messages = UserMessage.objects.all()
# all_messages = UserMessage.objects.filter(name = 'zone',address='北京')
# for message in all_messages:
# print message.name
# user_message = UserMessage()
# user_message.name = "zone3"
# user_message.message = "helloworld3"
# user_message.address = "合肥"
# user_message.email = "3@3.com"
# user_message.object_id= "helloworld3"
# user_message.save()
if request.method == "POST":
name = request.POST.get('name','')
message = request.POST.get('message','')
address = request.POST.get('address','')
email = request.POST.get('email','')
user_message = UserMessage()
user_message.name = name
user_message.message = message
user_message.address = address
user_message.email = email
user_message.object_id= "helloworld4"

user_message.save()
  return render(request, 'message_form.html')

删除数据:

def getform(request):
#all_messages = UserMessage.objects.all()
all_messages = UserMessage.objects.filter(name = 'zone',address='北京')
#all_messages.delete() #全部删掉
for message in all_messages:
message.delete() #s删除单条
print message.name

发表评论

邮箱地址不会被公开。