【问题标题】:Django:Error trying to add data in postgres databaseDjango:尝试在 postgres 数据库中添加数据时出错
【发布时间】:2021-10-28 09:21:34
【问题描述】:

每当我尝试通过 insert.html 模板向我的数据库插入数据时,我都会收到一个错误(视图 crud.views.insertEmp 没有返回 HttpResponse 对象。它返回的是 None。)。我正在使用 postgres 作为我的数据库。非常感谢您在识别错误​​方面的任何帮助。

models.py

from django.db import models


class Employee(models.Model):
    emp_name = models.CharField(max_length=100)
    email = models.CharField(max_length=100)
    occupation = models.CharField(max_length=100)
    salary = models.IntegerField()
    gender = models.CharField(max_length=1)

    class Meta:
        db_table = 'employee'

views.py

from django.shortcuts import render, redirect
from crud.models import Employee
from django.contrib import messages


def showEmp(request):
    employee = Employee.objects.all()
    context = {"data": employee}
    return render(request, "index.html", context)


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')
            saveRecord.salary = request.POST.get('salary')
            saveRecord.gender = request.POST.get('gender')
            saveRecord.save()
            messages.success(request, "Employee " +
                             saveRecord.emp_name + "  is added succesfully.")
        return render(request, "insert.html")

    else:
        return render(request, "insert.html")

inser.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</option>
                            <option value="">HR</option>
                            <option value="">Sales Manager</option>
                            <option value="">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>

【问题讨论】:

    标签: python django postgresql templates


    【解决方案1】:

    你构建代码的方式可能会导致 None 的返回,试试这个例子:

    from django.shortcuts import render, redirect
    
    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')
                saveRecord.salary = request.POST.get('salary')
                saveRecord.gender = request.POST.get('gender')
                saveRecord.save()
                messages.success(request, "Employee " +
                             saveRecord.emp_name + "  is added succesfully.")
            
                # Redirect to the same page (you can change the page to fit your needs)
                return redirect('.')
            else:
                # Not all required fields are present
                messages.error(request, "Please fill all the fields")
                # Also redirect the user to the same page
                return redirect('.')
    
        else:
            return render(request, "insert.html")
    

    【讨论】:

    • 它仍然没有保存在数据库中,else语句有效,这意味着记录没有被保存
    • 尝试在控制台登录request.POST查看是否所有字段设置正确
    • 这是日志:使用 CONTROL-C 退出服务器。 内部服务器错误:/insert Traceback(最近一次调用最后):文件“/home/mark/dev/employee/lib/python3.8/ site-packages/django/core/handlers/exception.py",第 47 行,内部响应 = get_response(request)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多