Django2.2 学习笔记 (6)_从数据库提取数据展示到前端html页面中

 1、从数据库提取数据展示到前端html页面中

if request.method =="GET":
    var_dict={}
    all_messages = Message.objects.all()
    # 如果不为空
    if all_messages:
        message = all_messages[0]
        var_dict = {"message": message}

    return render(request, "message_form.html",var_dict)

2、完整版views.py页面

from django.shortcuts import render

# Create your views here.
from apps.message_form.models import Message

def message_form(request):
    if request.method =="GET":
        var_dict={}
        all_messages = Message.objects.all()
        # 如果不为空
        if all_messages:
            message = all_messages[0]
            var_dict = {"message": message}
            return render(request, "message_form.html",var_dict)

    if request.method == "POST":
        name=request.POST.get("name","")
        address = request.POST.get("address", "")
        email = request.POST.get("email", "")
        message_txt = request.POST.get("message", "")

        message = Message()
        message.name = name
        message.address =address
        message.email =email
        message.message= message_txt
        message.save()
        return render(request, "message_form.html", {"message": message})
        

2、完整版前端页面





    
    
    


留言信息 请留下你的信息.

{%csrf_token%}

 

你可能感兴趣的:(Django)