Django没有将数据保存到数据库



我对Django有一个非常基本的了解,遇到一个问题,它让我完全困惑了。

我正在尝试构建一个3D绘图仪。一切都呈现正常,但当我点击提交时,它永远不会保存到数据库中,除了指定的默认值

我的<<p> strong> models.py
from django.db import models
class Value(models.Model):
eq_input = models.CharField(max_length=20, default='x**2 + y**2')
color = models.CharField(max_length=20, default='Magma')
我的<<p> strong> forms.py
from django import forms
from .models import Value
class ViewForm(forms.ModelForm):
Equation = forms.CharField(max_length=20, label='Equation')
Color = forms.CharField(max_length=20,label='Color')
class Meta:
model = Value
fields = {
'Equation',
'Color'
}
我的<<p> strong> views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Value
from .forms import ViewForm
def home_view(request):
if request.method == "POST":
form = ViewForm(request.POST)
if form.is_valid():
form.save()
else:
form = ViewForm()
context = {
'form': form
}
return render(request, "home.html", context)
我的<<p> strong> home。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>3D Graph Plotter</title>
</head>
<body>
<center><h1>This is a 3D plotter</h1></center>
<center>
<form action="." method="POST">{% csrf_token %}
{{ form.as_p }}
<input type="submit" name="Save" />
</form>
</center>
</body>
</html>

and myurls.py

from django.contrib import admin
from django.urls import path, include
from equation.views import eq, home_view
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_view, name='hv')
]
你能给我指出问题来吗?

{{ form.errors }} {{ form.non_field_errors }}放入home.html中,查看临时发布到视图的数据中是否有任何错误。可能数据无效,密码太常见或其他原因。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>3D Graph Plotter</title>
</head>
<body>
<center><h1>This is a 3D plotter</h1></center>
<center>
<form action="." method="POST">{% csrf_token %}
{{ form.as_p }}
{{ form.errors }}
{{ form.non_field_errors }}
<input type="submit" name="Save" />
</form>
</center>
</body>
</html>

最新更新