You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django使用Postgres数据库插入数据时视图返回None错误求助

问题定位&修复方案

核心错误点

  • 模板文件名拼写错误:你本地保存的模板文件名为inser.html(少了字母t),但视图中引用的是insert.html,模板找不到的异常会打断正常返回流程,导致视图返回None
  • 模板内表单字段错误:
    1. 员工姓名输入框多了多余的双引号:name="emp_name"",导致该字段提交时参数名解析异常,后端无法拿到emp_name的传入值
    2. 职业下拉框所有<option>的value属性都为空,不管用户选中哪个职业,后端拿到的occupation值都是空字符串,无法通过你写的字段非空校验
  • (可选优化)插入数据成功后直接render页面会导致刷新重复提交,建议改为重定向

修复后的代码

1. 模板文件(记得把文件名改为insert.html)

<center>
        <h1>Insert data into Postgresql</h1>
        <hr>

        <form method="POST">
            {% csrf_token %}
            <table border="">

                <tr>
                    <td>Employee Name</td>
                    <td><input type="text" placeholder=" Enter employee name.." name="emp_name"></td>
                </tr>

                <tr>
                    <td>Email</td>
                    <td><input type=" text" placeholder="Enter email" name="email">
                    </td>
                </tr>

                <tr>
                    <td>Occupaton</td>
                    <td>
                        <select name="occupation">
                            <option selected disabled=true>-- Select occupation --</option>
                            <option value="Programmer">Programmer</option>
                            <option value="HR">HR</option>
                            <option value="Sales Manager">Sales Manager</option>
                            <option value="Designer">Designer</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td>Salary</td>
                    <td><input type="text" placeholder="Enter salary" name="salary"></td>
                </tr>

                <tr>
                    <td>Gender</td>
                    <td>
                        <input type="radio" name="gender" value="M">Male |
                        <input type="radio" name="gender" value="F">Female
                    </td>
                </tr>

                <tr>
                    <td><input type="submit" value="Insert"></td>
                    <td>
                        {% if messages %}
                        {% for message in messages %}
                        <b style="color:green;">{{message}}</b>
                        {% endfor %}
                        {% endif %}
                    </td>
                </tr>
            </table>
        </form>
    </center>

2. 修复后的views.py中insertEmp函数

def insertEmp(request):
    if request.method == "POST":
        if request.POST.get('emp_name') and request.POST.get('email') and request.POST.get('occupation') and request.POST.get('salary') and request.POST.get('gender'):
            saveRecord = Employee() 
            saveRecord.emp_name = request.POST.get('emp_name')
            saveRecord.email = request.POST.get('email')
            saveRecord.occupation = request.POST.get('occupation')
            # salary是整数字段,转成int再赋值避免类型错误
            saveRecord.salary = int(request.POST.get('salary'))
            saveRecord.gender = request.POST.get('gender')
            saveRecord.save()
            messages.success(request, "Employee " +
                             saveRecord.emp_name + "  is added succesfully.")
            # 插入成功后重定向,避免重复提交
            return redirect("insertEmp")
        return render(request, "insert.html")
    else:
        return render(request, "insert.html")

内容的提问来源于stack exchange,提问作者Jumba Mark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 01:24:04