模型表单数据未显示在数据库中。表格不保存数据
Model form data not showing up in database. Form not saving data
我创建的表单没有将数据插入我的数据库 table。据我所知,我已经正确地完成了所有操作,但它仍然拒绝保存到数据库中。相反,它在控制台中显示“post”并清除表单字段而不在数据库中创建任何内容。 None 的正在输入的数据正在保存在任何地方?考虑到我已正确完成所有操作,这非常令人困惑。
ps。我已经连接了我的数据库,运行 迁移并创建了一个超级用户,但仍然没有。
models.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
forms.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__'
views.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 %}
两次更正:
第一个:在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':
我创建的表单没有将数据插入我的数据库 table。据我所知,我已经正确地完成了所有操作,但它仍然拒绝保存到数据库中。相反,它在控制台中显示“post”并清除表单字段而不在数据库中创建任何内容。 None 的正在输入的数据正在保存在任何地方?考虑到我已正确完成所有操作,这非常令人困惑。
ps。我已经连接了我的数据库,运行 迁移并创建了一个超级用户,但仍然没有。
models.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
forms.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__'
views.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 %}
两次更正:
第一个:在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':