使用django将html与mysql数据库绑定时出错

fcipmucu  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(257)
from django.shortcuts import render
from Insertemp.models import EmpInsert
from django.contrib import messages

# from django.http import HttpResponse

def Insertrecord(request):

    if request.method=='POST':
        if request.POST.get('epname')and request.POST.get('email')and request.POST.get('country'):    
            saverecord=EmpInsert()
            saverecord.empname=request.POST.get('empname')
            saverecord.email=request.POST.get('email')
            saverecord.country=request.POST.get('country')
            saverecord.save()
            message.success(request,'Record Saved Successfully...!')

            return render(request,'Index.html')
    else:   

        return render(request,'Index.html')

     [views.py file][1]

django在从html表单提交submit按钮后将html表单与mysql数据库绑定时,我在/view insertemp.views.insertrecord处获得错误值error,但没有返回httpresponse对象。它没有返回任何结果。申请方式:邮寄

sqxo8psd

sqxo8psd1#

如果您在post请求中,但缺少一个参数,则不会返回任何内容。

def Insertrecord(request):

    if request.method=='POST':

        if request.POST.get('epname')and request.POST.get('email')and request.POST.get('country'):    
            saverecord=EmpInsert()
            saverecord.empname=request.POST.get('empname')
            saverecord.email=request.POST.get('email')
            saverecord.country=request.POST.get('country')
            saverecord.save()
            message.success(request,'Record Saved Successfully...!')

            return render(request,'Index.html')

        else: # HERE IS THE MISSING ELSE
            return render(request,'Index.html') # OR RETURN SOMETHING ELSE

    else:   

        return render(request,'Index.html')

     [views.py file][1]

编辑-关于消息:
您必须导入邮件: from django.contrib import messages 你打字打错了 message 而不是 messages .

相关问题