1

我创建的表单没有将数据插入到我的数据库表中。据我所知,我已经正确完成了所有操作,但它仍然拒绝保存到数据库中。相反,它在控制台中“发布”并清除表单字段而不在数据库中创建任何内容。正在输入的数据都没有保存在任何地方?考虑到我已经正确完成了所有操作,这非常令人困惑。

附言。我已经连接了我的数据库,运行了迁移并创建了一个超级用户,但仍然没有。

模型.py

from django.db import models


Media_Choices = (
      ("TV", "TV"),
      ("Radio", "Radio"),
      ("Youtube", "Youtube"),
      ("Podcast", "Podcast"),
)

class Appear(models.Model):
   Show = models.CharField(max_length=100)
   Media = models.CharField(max_length=30, blank=True, null=True, choices=Media_Choices)
   Episode = models.IntegerField()
   Date = models.DateField(max_length=100)
   Time = models.TimeField(auto_now=False, auto_now_add=False)
   Producer = models.CharField(max_length=100)
   Producer_Email = models.EmailField(max_length=254)

def __unicode__(self):
   return self.Show + ' ' + self.Producer_Email

表格.py

from django import forms
from django.core.exceptions import ValidationError
from django.forms import ModelForm
from .models import Appear

class AppsForm(ModelForm):
    class Meta:
    model = Appear
    fields = '__all__'

视图.py

from django.shortcuts import redirect, render
from django.http import HttpResponse
from .forms import AppsForm
from .models import Appear


def AppS(request):
  if request == 'POST':
     form = AppsForm(request.POST)
     if form.is_valid():
      Apps = form.save(Commit=False)
      Apps.save()
else:
 form = AppsForm()
 return render(request, 'AppsForm.html', {'form': form})

def results(request):
return render(request, 'Results.html')

AppsForm.html

<body>
{% extends 'base.html' %}

{% block content %}

{% load crispy_forms_tags %}

<form action="" method="POST">

{% csrf_token %}

{{ form|crispy }}

<input type="submit" value="submit">
</form>
{% endblock %}
4

1 回答 1

3

二更正:

第一:在AppsForm.html

<form action="" method="POST">

向您的表单提供一个url,它应该在其中提交数据

<form action="{% url 'url_name_for_Apps_view' %}" method="POST">

第二:在AppS view

这应该是

if request == 'POST':
if request.method == 'POST':
于 2021-09-06T08:56:55.553 回答