Search code examples
pythondjangodatabasedjango-formsdjango-model-field

Model form data not showing up in database. Form not saving data


The form I created is not inserting the data into my database table. As far as I can tell I've done everything correctly but it still refuses to save into the database. Instead it "post" in the console and clears the form fields without creating nothing in the database. None of the data that is being entered is being saved anywhere? This is extremely confusing considering that I've done everything correctly.

ps. I've connected my database, ran migrations and created a superuser as well but still nothing.

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 %}

Solution

  • Two Correction :

    First : In AppsForm.html

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

    Provide a url to your form, where it should submit data

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

    Second : In AppS view

    This should be

    if request == 'POST':
    
    if request.method == 'POST':