python - 模型表格无法保存在数据库中
问题描述
我是 Django 的初学者,我想将表单数据保存在数据库中,但我无法保存,也遵循了一些教程。
表格.py:
from django.forms import ModelForm
from .models import *
class listsForm(ModelForm):
class Meta:
model = todo
fields = "__all__"
视图.py:
from django.shortcuts import render
from .models import *
from .form import *
def index(request):
lists = todo.objects.all()
form = listsForm()
context = {
'lists':lists,
'form':form,
}
if request.method == 'POST':
form = listsForm(request.POST)
if form.is_valid:
form.save()
return render(request, 'index.html', context)
模型.py:
from django.db import models
class todo(models.Model):
title = models.CharField(max_length=200)
description = models.TextField(null=True, blank=True)
created = models.DateField(auto_now_add=True)
def __str__(self):
return self.title
解决方案
我找到了为什么它不起作用,
我正在使用<input type="button">
提交按钮,但是当我将其更改为它时,<button type="submit">
它可以工作。
推荐阅读
- bash - 用 `jQuery` 用 bash 替换所有 $ 符号
- python - 在外壳中显示一个条形以直观地指示不断变化的计数
- javascript - 我如何删除不是我的消息,而是将机器人发送给我的消息?
- scala - 将 Scala Seq 转换为 Spark DStream?
- powerquery - Expression.Error:我们无法将值 3 转换为函数类型
- react-native - React Native Reanimated 2 为 SVG 路径的长度设置动画
- python - Pyinstaller 不包括 ffmpeg.exe 和 ffprobe.exe
- r - 在 Stata 和 R 之间具有时变系数的 coxph 的不同结果
- html - 网格容器的意外滚动
- azure-devops - 如何按标签显示错误?