我经常遇到这个错误-视图location_form.views.Insertrecord没有返回HttpResponse对象,而是返回None。
我尝试了所有可能的缩进方式。我用类似的方式创建了另一个视图,并且成功了。有人能告诉我哪里出错了吗?
我想把这个表单的数据存储在我的数据库中,这个数据库是phpmyadmin(mysql)
views.py
from django.shortcuts import render
from .models import Location
from django.contrib import messages
# Create your views here.
def Insertrecord(request):
if request.method=='POST':
if request.POST.get('id') and request.POST.get('parent_id') and request.POST.get('name') and request.POST.get('status') and request.POST.get('added_by') and request.POST.get('updated_by') and request.POST.get('created_on') and request.POST.get('updated_on') :
saverecord=Location()
saverecord.id=request.POST.get('id')
saverecord.parent_id=request.POST.get('parent_id')
saverecord.name=request.POST.get('name')
saverecord.status=request.POST.get('status')
saverecord.added_by=request.POST.get('added_by')
saverecord.updated_by=request.POST.get('updated_by')
saverecord.created_on=request.POST.get('created_on')
saverecord.updated_on=request.POST.get('updated_on')
saverecord.save()
messages.success(request,"Record Saved Successfully..!")
return render(request, 'location.html')
else:
return render(request,'location.html')
location.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Location Registration Form</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<nav class="navbar bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">
<img src="https://img.freepik.com/premium-vector/customer-service-icon-vector-full-customer-care-service-hand-with-persons-vector-illustration_399089-2810.jpg?w=2000" alt="" width="30" height="24" class="d-inline-block align-text-top">
Location Registration Portal
</a>
</div>
</nav>
</head>
<body background="https://img.freepik.com/free-vector/hand-painted-watercolor-pastel-sky-background_23-2148902771.jpg?w=2000">
<br>
<h1 align="center">Location Registration Portal</h1>
<hr>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
<div class="container-sm">
<form method = "POST">
{% csrf_token %}
<div class="row mb-3">
<label for="inputEmail3" class="col-sm-2 col-form-label">Country</label>
<div class="col-sm-10">
<input type="text" name="name" class="form-control" id="inputEmail3"/>
</div>
</div>
<div class="row mb-3">
<label for="inputEmail3" class="col-sm-2 col-form-label">Status</label>
<div class="col-sm-10">
<select class="form-select" aria-label="Default select example">
<option selected>--Select Status--</option>
<option value="1">Active</option>
<option value="0">Inactive</option>
</select>
<br><br>
{% if messages %}
{% for message in messages %}
<h2 style="color: green;">{{message}}</h2>
{% endfor %}
{% endif %}
<div class="d-grid gap-2 col-6 mx-auto">
<input style="color: black;" type="submit" value="Submit"/>
</div>
<br>
</form></div>
</body>
</html>
网址. py
from django.contrib import admin
from django.urls import path
from customer_form import views as cview
from location_form import views as lview
urlpatterns = [
path('admin/', admin.site.urls),
# path('', admin.site.login, name='login'),
path('', cview.Insertrecord),
path('location/', lview.Insertrecord),
]
1条答案
按热度按时间dxxyhpgq1#
它没有给出任何值,因为您正在if语句中呈现模板。