首页 > 解决方案 > 为了使 html 表单附加到 mysql 数据库,我得到错误̥,它说:异常类型:ValueError 异常值

问题描述

运行服务器后,我将我的 html 表单绑定到 django 中的 mysql 数据库表,该表单在浏览器中可见,但是单击提交后会发生此错误,即填写表单并单击提交按钮后出现以下错误

Exception Type: ValueError
Exception Value:    
The view Insertemp.views.Insertrecord didn't return an HttpResponse object. It returned None instead.
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('pname')and request.POST.get('pcategory')and request.POST.get('pdetails')and request.POST.get('foundedin')and request.POST.get('orderoftest')and request.POST.get('t1')and request.POST.get('t2')and request.POST.get('t3')and request.POST.get('f1')and request.POST.get('f2')and request.POST.get('f3')and request.POST.get('f4')and request.POST.get('f5'):    

            saverecord=EmpInsert()

            saverecord.pname=request.POST.get('pname')

            saverecord.pcategory=request.POST.get('pcategory')

            saverecord.pdetails=request.POST.get('pdetails')

            saverecord.foundedin=request.POST.get('foundedin')

            saverecord.foundedin=request.POST.get('orderoftestimonial')

            saverecord.t1=request.POST.get('t1')

            saverecord.t2=request.POST.get('t2')

            saverecord.t3=request.POST.get('t3')

            saverecord.f1=request.POST.get('f1')

            saverecord.f2=request.POST.get('f2')

            saverecord.f3=request.POST.get('f3')

            saverecord.f4=request.POST.get('f4')

            saverecord.f5=request.POST.get('f5')
            saverecord.save()

            messages.success(request,'Record Saved Successfully...!')
            
            
            
            

            return render(request,'Index.html')
        

    else:        
    

         return render(request,'Index.html')

     

            
           

标签: pythonmysqldjangoweb

解决方案


您的检查中存在间隙,因此它不返回任何内容,因为它是一个发布请求,但未通过您的if数据检查;

def Insertrecord(request):
   

    if request.method=='POST':
        
        
        if request.POST.get('pname')and request.POST.get('pcategory')and request.POST.get('pdetails')and request.POST.get('foundedin')and request.POST.get('orderoftest')and request.POST.get('t1')and request.POST.get('t2')and request.POST.get('t3')and request.POST.get('f1')and request.POST.get('f2')and request.POST.get('f3')and request.POST.get('f4')and request.POST.get('f5'):    

            ...

            messages.success(request,'Record Saved Successfully...!')
            
            return render(request,'Index.html')

        # Here it returns None because it's a POST request 
        # but not into the above block
        # return something at this point to avoid the black hole
        return render(request, 'Index.html')
    else:        
        return render(request,'Index.html')

您可能还应该将一些上下文传递回,render以便您可以在渲染模板后识别视图中发生的事情。


推荐阅读