KyleStranger
KyleStranger

Reputation: 231

Django Model Form not saving to database (No error message)

As per the above (title) my Django form is not saving, however, it does redirect to the correct page, the entry does not show on the admin page.

Please see the below code :

Views.py:

def newSetting(request):
    form = SettingsForm()

    if request.method == 'POST':
        form = SettingsForm(request.POST)
        if form.is_valid():
            form.save()

    return render(request , 'main/newSetting.html' , {'form':form})

.HTML:

{% extends "main/base.html"%}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">

<style >
  .row_container{
    line-height: 500%;
  }
</style>
{% block content %}
<form class="" action="{% url 'settingsHome' %}" method="post">
  {% csrf_token %}
  {{ form.Complex }}
  <br>
  <br>
  <div class="row_container" name='TRB-YTD'>
    <div class="row mb-.1">
      <div class="col" style="left-align">
          {{ form.Trial_balance_Year_to_date }}
      </div>
      <div class="col-11">
        <p> Trial balance YTD</p>
      </div>
    </div>
  </div>
  <div class="row_container" name="TRB-MONTH">
    <div class="row ">
      <div class="col" style="left-align">
          {{ form.Trial_balance_Monthly }}
      </div>
      <div class="col-11">
        <p> Trial balance Monthly</p>
      </div>
    </div>
  </div>
  <div class="row_container" name="IS-YTD">
    <div class="row ">
      <div class="col" style="left-align">
          {{ form.Income_Statement_Year_to_date }}
      </div>
      <div class="col-11">
        <p> Income Statement YTD</p>
      </div>
    </div>
  </div>
  <div class="row_container" name="IS-MONTHLY">
    <div class="row ">
      <div class="col" style="left-align">
          {{ form.Income_Statement_Monthly }}
      </div>
      <div class="col-11">
        <p> Income Statement Monthly</p>
      </div>
    </div>
  </div>
  <div class="row_container" name="AGE-ANALYSIS">
    <div class="row ">
      <div class="col" style="left-align">
          {{ form.Age_Analysis }}
      </div>
      <div class="col-11">
        <p> Age Analysis</p>
      </div>
    </div>
  </div>
  <div class="row_container" name="BAL-SHEET">
    <div class="row ">
      <div class="col" style="left-align">
          {{ form.Balance_Sheet }}
      </div>
      <div class="col-11">
        <p> Balance Sheet</p>
      </div>
    </div>
  </div>
  <div class="row_container" name="REP-MAIN-GL">
    <div class="row ">
      <div class="col" style="left-align">
          {{ form.Repair_and_Maintenance_General_Ledger }}
      </div>
      <div class="col-11">
        <p> Repair & Maintenance GL</p>
      </div>
    </div>
  </div>
  <div class="row_container" name="MAJOR-CAPTAL-GL">
    <div class="row ">
      <div class="col" style="left-align">
          {{ form.Mayor_capital_Items_General_Ledger }}
      </div>
      <div class="col-11">
        <p> Major Capital Items GL</p>
      </div>
    </div>
  </div>

<button type="submit" class="btn btn-primary" name="btn-NewSetting"> Save New Setting</button>

</form>
{% endblock %}

Forms.py:

from django.forms import ModelForm
from .models import SettingsClass

class SettingsForm(ModelForm):
     class Meta:
         model = SettingsClass
         fields = '__all__'

Models.py:

class SettingsClass(models.Model):
    Complex = models.CharField(choices=complex_list , max_length = 2 ,default='1')
    Trial_balance_Year_to_date= models.BooleanField(default = False)
    Trial_balance_Monthly=models.BooleanField(default = False)
    Income_Statement_Year_to_date=models.BooleanField(default = False)
    Income_Statement_Monthly=models.BooleanField(default = False)
    Age_Analysis=models.BooleanField(default = False)
    Balance_Sheet=models.BooleanField(default = False)
    Repair_and_Maintenance_General_Ledger=models.BooleanField(default = False)
    Mayor_capital_Items_General_Ledger=models.BooleanField(default = False)
    def __str__(self):
        return (self.Complex + ' Settings')

Admin.py:

from django.contrib import admin
from .models import SettingsClass
# Register your models here.


class SettingsAdmin(admin.ModelAdmin):
    pass
admin.site.register(SettingsClass, SettingsAdmin)

If any body knows what could cause this , please assist , there are no errors showing when the form is submitted...

Upvotes: 1

Views: 3601

Answers (1)

Echo Foe
Echo Foe

Reputation: 498

Add the template test.html to the catalog and try this in views.py, to check whether the form is saved or not, and so use the print() function in the console to find out if the program is being executed at one stage or another:

def newSetting(request):
form = SettingsForm()

if request.method == 'POST':
    form = SettingsForm(request.POST)
    if form.is_valid():
        form.save()
        return render(request , 'main/test.html' , {'form':form})

return render(request , 'main/newSetting.html' , {'form':form})

Upvotes: 1

Related Questions