首页 > 解决方案 > 模型表格无法保存在数据库中

问题描述

我是 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

标签: pythondjangodatabaseweb

解决方案


我找到了为什么它不起作用,

我正在使用<input type="button">提交按钮,但是当我将其更改为它时,<button type="submit">它可以工作。


推荐阅读