Exemplo n.º 1
0
def message_form(request):
    if request.method == "POST":
        name = request.POST.get("name","")
        email = request.POST.get("email", "")
        address = request.POST.get("address", "")
        message_text = request.POST.get("message_text", "")

        message = Message()
        message.name = name
        message.email = email
        message.address = address
        message.message = message_text

        message.save()

        return render(request,"message_form.html",{"message":message})

    if request.method == "GET":
        var_dict = {}
        all_message = Message.objects.all()
        if all_message: #判断all_message是否存在 暨数据库中是否有数据
            message = all_message[0] #【0】列表中第一个数据
            var_dict = {
                "message":message #前一个message是key 对应html离 后一个message是value
            }

        return render(request,"message_form.html", var_dict)
Exemplo n.º 2
0
def message_form(request):
    if request.method == "POST":
        name = request.POST.get("name", "")
        email = request.POST.get("email", "")
        memo = request.POST.get("message", "")
        message = Message()
        message.name = name
        message.email = email
        message.message = memo
        message.save()
        return render(request, "index.html", {
            "Message": message
        })

    if request.method == "GET":
        template = loader.get_template('index.html')
        var_dict = {}
        all_message = Message.objects.all()
        if all_message:
            message = all_message[0]
            var_dict = {
                "Message": message
            }
        # return render(request, "index.html", var_dict)
        return HttpResponse(template.render(var_dict))
Exemplo n.º 3
0
def message_form(request):

    if request.method == 'POST':

        name = request.POST.get('name', '')
        email = request.POST.get('email', '')
        address = request.POST.get('address', '')
        messagestr = request.POST.get('message', '')

        message = Message()
        message.name = name
        message.email = email
        message.address = address
        message.message = messagestr
        message.save()
        return render(request, "message_form.html")

    if request.method == 'GET':

        message = Message.objects.filter()
        if message:
            message = message[0]
            return render(request, "message_form.html", {"message": message})
        else:
            return render(request, "message_form.html")
Exemplo n.º 4
0
def message_form(request):
    # queryset 1.进行for循环 2.进行切片
    # queryset本身并没有执行sql操作
    # all_messages = Message.objects.all()
    # sliced_query = Message.objects.all()[:1]
    # print(all_messages.query)
    # print(sliced_query.query)

    # 2.filter 同样返回的是queryset结果集
    # all_messages = Message.objects.filter(name='bobby')
    # print(all_messages.query)
    # for message in all_messages:
    #     print(message.name)

    # 3.get 返回的是一个对象,数据不存在或者有多条数据存在会抛出异常
    # queryset 在进行for循环的时候才会查询数据库,而set在调用的时候立马就会查询数据库
    # message = Message.objects.get(name="bobby")
    # print(message.name)

    # 进行数据插入操作
    # message = Message()
    # message.name = 'bobby'
    # message.email = '*****@*****.**'
    # message.address = '北京市昌平区'
    # message.message = '留言'
    #
    # message.save() # 1.如果记录存在则更新,如果不存在则插入

    # 从html页面获取数据并送入数据库中
    if request.method == 'POST':
        name = request.POST.get('name', '')
        email = request.POST.get('email', '')
        address = request.POST.get('address', '')
        message_text = request.POST.get('message', '')

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

    if request.method == 'GET':
        var_message = {}
        all_messages = Message.objects.all()
        if all_messages:
            message = all_messages[0]
            var_message = {
                'message': message,
            }
        return render(request, 'message_form.html', var_message)
Exemplo n.º 5
0
def message_form(request):
    # # 1. 用all,返回的是queryset,可以用for循环遍历,也可以做切片
    # all_messages = Message.objects.all()
    #
    # # 2. 用filter,相当于where语句,会找出所有符合条件的,如果没有的话,返回空queryset
    # all_messages = Message.objects.filter(name='wangi')
    # # 删除得到的数据
    # # all_messages.delete()
    #
    # for message in all_messages:
    #     # 逐条删除数据
    #     # message.delete()
    #     print(message.name)
    #
    # # 3. 用get,返回的是一个对象,不能用for循环遍遍历,如果找不到对象,会抛出异常
    # message = Message.objects.get(name = 'wangi')
    # print(message.address)
    #
    # # 进行数据插入操作
    # message = Message()
    # message.name = 'zicheng'
    # message.email = '*****@*****.**'
    # message.address = 'zicheng street zicheng room'
    # message.message = 'wangi is beautiful'
    # # 如果存在则更新,如果不存在则插入
    # message.save()
    if request.method == 'POST':
        name = request.POST.get("name", "")
        email = request.POST.get("email", "")
        address = request.POST.get("address", "")
        content = request.POST.get("message", "")

        message = Message()
        message.name = name
        message.email = email
        message.address = address
        message.message = content
        message.save()

        var_dict = {"message": message}

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

    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)
Exemplo n.º 6
0
def message_form(request):
    # queryset 1.进行for循环 2.进行切片
    # queryset本身没有执行sql操作

    # all_messages = Message.objects.all()[:1]
    # print(all_messages)

    # 2.filter
    # all_messages = Message.objects.filter(name='jack')
    # all_messages.delete()  删除全部数据
    # print(all_messages.query)
    # for message in all_messages:
    #     print(message.name)

    # 3.get 返回的是一个对象,数据不存在或者有多条数据存在会抛出异常
    # try:
    #     message = Message.objects.get(name='jack')
    # except Message.DoesNotExist as e:
    #     pass
    # except Message.MultipleObjectsReturned as e:
    #     pass
    # message.delete() 删除该条数据
    # print(message.name)

    # 进行数据插入操作
    message = Message()
    # message.name = 'kiven'
    # message.email = '*****@*****.**'
    # message.address = 'CA'
    # message.message = 'hi'
    # # 如果存在则更新,如果不存在则插入
    # message.save()

    # 从html中提取数据保存到数据库中
    if request.method == 'POST':
        name = request.POST.get('name', '')
        email = request.POST.get('email', '')
        address = request.POST.get('address', '')
        message_text = request.POST.get('message', '')

        message = Message()
        message.name = name
        message.email = email
        message.address = address
        message.message = message_text
        message.save()

    return render(request, 'message_form.html')
Exemplo n.º 7
0
def message_form(request):
    if request.method == 'POST':
        name = request.POST.get('name', '')
        email = request.POST.get('email', '')
        address = request.POST.get('address', '')
        message_text = request.POST.get('message', '')

        message = Message()
        message.name = name
        message.email = email
        message.address = address
        message.message = message_text
        message.save()
        return render(request, "message_form.html", {"message": message})
    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)
        else:
            return render(request, "message_form.html")
Exemplo n.º 8
0
def message_form(request):
    # 从models中获取数据
    all_messages = Message.objects.all()  # 获取所有数据
    # 切片操作
    # all_messages = Message.objects.all()[:1]  获取第1个数据
    # 遍历
    # for message in all_messages:
    #     print(message.name, message.email, message.address, message.message)
    #     print(all_messages.query)

    # filter
    # all_messages = Message.objects.filter(name='zhf')
    # all_messages = Message.objects.filter(email='[email protected]')
    # for message in all_messages:
    #     print(message.name)
    #     print(all_messages.query)

    # get
    # message = Message.objects.get(name='zhf')
    # 找到多个结果时会抛异常
    # 偷懒的异常捕获写法,不推荐
    # try:
    #     message = Message.objects.get(email='[email protected]')
    #     print(message.email)
    # except Exception as e:
    #     print(e)

    # try:
    #     message = Message.objects.get(email='[email protected]')
    #     print(message.email)
    # except Message.MultipleObjectsReturned as e:
    #     print(e,'发生异常,get返回了多条数据')
    # except Message.DoesNotExist as e:
    #     print(e,'发生异常,查找的数据不存在')

    # delete方法
    # all_messages = Message.objects.filter(name='zhf')
    # all_messages.delete()   # 将用filter中满足条件的数据全部删除

    # save方法,不存在时是创建数据,存在时是更新数据
    # message = Message()
    # message.name='test'
    # message.address="测试地址"
    # message.email="测试邮件地址abc"
    # message.message="测试的消息内容"
    # message.save()

    # 点击了提交按钮
    if request.method == "POST":  # 点击submit按钮时会是一个post请求  <input type="submit" class="button" value="提交"/>
        print("POST")
        # 获取页面提交的数据
        # 后面的空值,是给一个默认值,如果这个值不存在,就给个空,这样不容易出异常
        name = request.POST.get(
            'name', ''
        )  #<input id="name" type="text" name="name" class="error" placeholder="请输入您的姓名"/>
        email = request.POST.get(
            'email', ''
        )  #  <input id="email" type="email" value="" name="email" placeholder="请输入邮箱地址"/>
        address = request.POST.get('address', '')
        message_text = request.POST.get('message', '')
        print(name, email, address, message_text)

        # 将数据写入数据库
        #创建数据对象
        message = Message()  #创建模型对象(model_instance)
        message.name = name
        message.email = email
        message.address = address
        message.message = message_text
        if name:
            #将数据写入数据库
            message.save()
            print('成功保存一条记录')

    # 从数据库获取数据以便于显示在页面上
    if request.method == "GET":
        print("GET")
        # 读取数据库,在这里直接取第1个数据
        all_messages = Message.objects.all()
        print(all_messages[0].name)
        if all_messages:  # 如果不为空列表
            message = all_messages[0]
            print(message)
            # name = message.name
            # email = message.email
            # address = message.address
            # message_text = message.message
            # print(name,email,address,message_text)
            return render(
                request,
                "message_form.html",
                {
                    "message":
                    message  #返回key:value形式的数据, 在message_form.html中可以用Django提供的模板语言引用这些数据
                })
        # else:
        #     print('all_message is null')

    return render(request, "message_form.html")
Exemplo n.º 9
0
def message_form(request):
    # Message类中注入了一个objects对象,他是所有操作的核心
    # 它可以取数据,all()方法取出所有、

    # 在return前打断点,使用dubug模式查看获取的对象 QuerySet
    # QuerySet对象是python中内置的一个类型,可以对他进行for循环
    # 对QuerySet进行切片操作,可以QuerySet对象当作一个list列表
    # all_message = Message.objects.all()[:1]
    # QuerySet只是执行了一条sql语句,本身没有执行sql操作
    # all_message=Message.objects.all()
    # sliced_message=Message.objects.all()[:1]
    # # SELECT `message`.`name`, `message`.`email`, `message`.`address`, `message`.`message` FROM `message`  LIMIT 1
    # print(sliced_message.query)
    # # SELECT `message`.`name`, `message`.`email`, `message`.`address`, `message`.`message` FROM `message`
    # print(all_message.query)
    # # 打印时才执行了sql操作
    # print(sliced_message.name)
    # --------------------------------------------------------
    # 2 . filter操作,相当于where语句
    # all_message = Message.objects.filter(name='wzy')
    # # SELECT `message`.`name`, `message`.`email`, `message`.`address`, `message`.`message` FROM `message` WHERE `message`.`name` = wzy
    # print(all_message.query)
    # # for循环里才执行了sql操作
    # for message in all_message:
    #     # message是all_message 的实例  Message object (bobby)
    #     print(message.name)
    # --------------------------------------------------------
    # 3. get方法,返回的是一个对象 而不是queryset
    # 只返回一条数据,要求比较严格,如果数据不存在或者有多条会抛出异常
    # 和返回QuerySet对象不同,get方法一执行就开始执行sql语句
    # try:
    #     message = Message.objects.get(name='wzy')
    #     print(message.email)
    # #     偷懒写法,所有的异常
    # # except Exception as e:
    # # 常规写法,具体到那种异常
    # except Message.DoesNotExist as e:
    #     pass
    # except Message.MultipleObjectsReturned as e:
    #     pass
    # 删除数据
    # 删除所有
    # message =Message.objects.all()
    # 使用get或filter删除一个
    # message = Message.objects.filter(name='wzy')
    # message.delete()
    # 新增数据 先实例化一个messages对象
    # messages = Message()
    # messages.name='ewe'
    # messages.email='*****@*****.**'
    # messages.address='ewwe'
    # messages.message='rrrrrrr'
    # # save()方法  如果数据存在则 修改,如果不存在则 新增
    # messages.save()

    # 从前端页面接受数据,通过debug模式,返回前端一个<WSGIRequest: POST '/message_form/'> 即WSGIRequest对象
    # 表单提交后,通过request.POST获取一个dict字典,通过get()方法获取val值
    if request.method == 'POST':
        name = request.POST.get('name', '')
        email = request.POST.get('email', '')
        address = request.POST.get('address', '')
        message_text = request.POST.get('message', '')
        message = Message()
        message.name = name
        message.email = email
        message.address = address
        message.message = message_text
        message.save()
        return render(request, 'message_form.html', {'message': message})
    if request.method == 'GET':
        var_dict = {}
        all_message = Message.objects.all()
        if all_message:
            message = all_message[0]
            var_dict = {'message': message}
        return render(request, 'message_form.html', var_dict)