python - 为了使 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')
解决方案
您的检查中存在间隙,因此它不返回任何内容,因为它是一个发布请求,但未通过您的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
以便您可以在渲染模板后识别视图中发生的事情。
推荐阅读
- c# - SSIS的脚本任务中可以省略Dts.TaskResult吗
- ios - 在 ios swift voicememo 应用程序中重新录制音频
- python - 调试 python 云堆栈模块中可能存在的错误的 ansible 跟踪
- javascript - 使 jquery datepicker 每年在相同的日期日期使用不同的颜色
- java - VisualVM - 我如何连接?
- azure - 天蓝色架构混合解决方案
- django - django.db.utils.IntegrityError: 1062, 运行 Django 迁移时键的重复条目
- azure-devops - 从 Azure DevOps (VSTS) 中的测试计划运行 SpecFlow 方案大纲
- css - 在子组件中覆盖父组件样式
- xml - 请参考问题“Escape backslash in XML and split as separate xml node using XSLT”