from . models import UserMessage
查询全部:UserMessage.objects.all()
过滤查询:UserMessage.objects.filter()
filter(name=“wang”,address=“content”)返回内容可以遍历
def getform(request):
all_messages = UserMessage.objects.filter(name="wang")
for messages in all_messages:
print(messages.name)
return render(request,"message_form.html")
#创建对象
user_message = UserMessage()
user_message.name = "wanglishuai"#属性赋值
user_message.message="body2"
user_message.address="地址3"
user_message.email="[email protected]"
user_message.object_id="123444"
#保存
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="6666"
user_message.save()
1、html文件中每个表单项的name即返回参数
2、使用request.POST.get(“参数名”,’ ')获取参数内容,如果未取到则取空值。
3、html文件中action="/总路径/form/" 如果调用这个html的函数在app内,并且使用了转发的urls方法,则在本app内的urls中注册form/,上方总路径则为注册的app的总路径。
4、在form中需要添加参数 {% csrf_token %} ,Django默认确认表单的安全可靠性,否则不予通过。