阿豪要卷死我 2021-12-31 17:19 采纳率: 50%
浏览 61
已结题

Django中POST请求无法传输到数据库

无法把request.POST.get取得的值,传递给Django ORM的create()函数,生成一条数据记录.

Views代码:
from django.shortcuts import render,redirect,HttpResponse
from .models import employee,department,group,employeeinfo
# Create your views here.
# 在此处编写视图函数代码
#部门数据表的增删查改
def list_dep_old(request):
    #取得数据表中的全部记录
    dep_list = department.objects.all()
    return render(request,'test_orm_old/list_dep_old.html',{'dep_list':dep_list})

def add_dep_old(request):
    #判断请求方式,如果是POST,说明前端页面需要提交数据
    if request.method == 'POST':
        dep_name = request.POST.get('dep_name')
        print(dep_name)
        dep_script = request.POST.get('dep_script')
        if dep_name.strip() == '':
            return render(request,'test_orm_old/add_dep_old.html',{'error_info':'部门名称部门为空!'})
        try:
            #用create()函数新建一条记录,这条记录会自动保存,不用调用save()函数
            p = department.objects.create(dep_name=dep_name,dep_script=dep_script)
            #ojb = department(dep_name=dep_name,dep_script=dep_script)
            #ojb.save()
            return redirect('/test_orm_old/list_dep_old/')
        except Exception as e:
            return render(request,'/test_orm_old/add_dep_old.html',{'error_info':'输入的部门名称重复或者信息错误!'})
        finally:
            pass
    return render(request,'test_orm_old/add_dep_old.html')

add_dep_old.html代码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>增加</title>
    </head>
    <body>
        <div align="center">
            <h1>增加部门</h1>
            <hr>
            <!--设置<from>表单的method="POST",当点击“保存按钮时”,向后端发送post请求-->
            <from action = "" method="post">
                <!--Django安全机制,防止CSRF-->
                {% csrf_token %}
                <input type="hidden" name="id" id="id" value="{{ department.id }}">
                <div>
                    <label>部门:</label>
                    <!--<input标签>的name属性必须设置,后端通过name属性取值-->
                    <input type="text" name="dep_name" id="dep_name">
                </div>
                <br>
                <div>
                    <label>备注:</label>
                    <input type="text" name="dep_script" id="dep_script">
                </div>
                <br>
                <div><input type="submit" value="保存"></div>
            </from>
            <!--错误信息显示在此处-->
            {{ error_info }}
        </div>
        <div align="center"><a href="/test_orm_old/list_dep_old/">返回部门列表</a></div>
    </body>
</html>

运行结果:输入信息点击保存没有任何反应。
  • 写回答

2条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 系统已结题 1月8日
      • 已采纳回答 12月31日
      • 创建了问题 12月31日

      悬赏问题

      • ¥15 关于selenium网页闪退
      • ¥15 怎么用PYTHON解类似于x = y+zx + cy = x+zy + v的方程
      • ¥300 FLASH AS2.0制作一个类似手机上下滑动一样的效果
      • ¥15 为什么使用openFoam 中的icoFoam计算圆柱扰流时出现浮点数例外(核心已转储)
      • ¥15 51单片机外部中断控制数码管
      • ¥15 创建网页里面的图片显示不出来
      • ¥15 语音控制的编写?(语言-c#)
      • ¥15 matlab输入书上的代码运行错误
      • ¥15 matlab怎么做三维曲面
      • ¥50 sph光滑粒子法能否解决阶跃性的问题