I currently have a settings model for my Django app which saves a set of settings for the rest of the program to read from.
I have tried to create a way to update these settings on the app, as per the below code.
When the update button is clicked, however, the app returns the 'error':'Bad info' variable instead of updating the database. Does anyone know what could be causing it because I cannot seem to find any errors in the code?
Views.py:
def viewSettings(request, settings_pk):
setting = get_object_or_404(SettingsClass, pk=settings_pk)
if request.method == 'GET':
form = SettingUpdateForm(instance=setting)
return render(request, 'main/viewSettings.html', {'setting': setting, 'form':form})
else:
try:
form = SettingUpdateForm(request.POST, instance=setting)
form.save()
return redirect('settingsHome')
except ValueError:
return render(request, 'main/viewSettings.html', {'setting': setting, 'form':form, 'error':'Bad info'})
Models.py:
class SettingsClass(models.Model):
Complex = models.CharField(choices=complex_list , max_length = 22 ,default='1' , unique=True)
#Trial Balance Year To Date
Trial_balance_Year_to_date= models.BooleanField(default = False)
tbytd_Include_opening_balances=models.BooleanField(default = False)
tbytd_Only_use_main_accounts=models.BooleanField(default = False)
tbytd_Print_null_values=models.BooleanField(default = False)
tbytd_Print_description=models.BooleanField(default = True)
tbytd_Print_account=models.BooleanField(default = True)
tbytd_Sort_by_account_name=models.BooleanField(default = True)
#Trial Balance Monthly
Trial_balance_Monthly=models.BooleanField(default = False)
tbm_Only_use_main_accounts=models.BooleanField(default = False)
tbm_Print_null_values=models.BooleanField(default = False)
tbm_Print_description=models.BooleanField(default = True)
tbm_Print_account=models.BooleanField(default = True)
tbm_Sort_by_account_name=models.BooleanField(default = True)
#Income Statement Year To Date
Income_Statement_Year_to_date=models.BooleanField(default = False)
isytd_Only_use_main_accounts=models.BooleanField(default = False)
isytd_Sort_by_account_name=models.BooleanField(default = True)
isytd_Print_null_values=models.BooleanField(default = False)
isytd_Print_description=models.BooleanField(default = True)
isytd_Print_account=models.BooleanField(default = True)
#Income Statement Monthly
Income_Statement_Monthly=models.BooleanField(default = False)
ism_Only_use_main_accounts=models.BooleanField(default = False)
ism_Sort_by_account_name=models.BooleanField(default = True)
ism_Print_null_values=models.BooleanField(default = False)
ism_Print_description=models.BooleanField(default = True)
ism_Print_account=models.BooleanField(default = True)
#Age Analysis
Age_Analysis=models.BooleanField(default = False)
aa_Include_all_customers=models.BooleanField(default = True)
aa_Include_cash_customers=models.BooleanField(default = True)
aa_include_on_hold_customers=models.BooleanField(default = True)
aa_include_null_values=models.BooleanField(default = False)
aa_allocate_unalocated_credits_to_oldest=models.BooleanField(default = True)
#Balance Sheet
Balance_Sheet=models.BooleanField(default = False)
bs_Only_use_main_accounts=models.BooleanField(default = False)
bs_Print_current_month=models.BooleanField(default = True)
bs_Include_null_values=models.BooleanField(default = False)
bs_Print_assets_first=models.BooleanField(default = True)
bs_statement_of_financial_position=models.BooleanField(default = False)
#Repair and Maintenance General Ledger
Repair_and_Maintenance_General_Ledger=models.BooleanField(default = False)
rmgl_Ignore_inactive_accounts=models.BooleanField(default = True)
rmgl_Sort_by_transaction_date=models.BooleanField(default = True)
rmgl_Group_by_account_type=models.BooleanField(default = True)
rmgl_Print_description=models.BooleanField(default = True)
rmgl_Print_account=models.BooleanField(default = True)
rmgl_Print_year_to_date=models.BooleanField(default = True)
rmgl_Print_monthly=models.BooleanField(default = False)
#Major Capital Items General Ledger
Major_capital_Items_General_Ledger=models.BooleanField(default = False)
mcigl_Ignore_inactive_accounts=models.BooleanField(default = True)
mcigl_Sort_by_transaction_date=models.BooleanField(default = True)
mcigl_Group_by_account_type=models.BooleanField(default = True)
mcigl_Print_description=models.BooleanField(default = True)
mcigl_Print_account=models.BooleanField(default = True)
mcigl_Print_year_to_date=models.BooleanField(default = True)
mcigl_Print_monthly=models.BooleanField(default = False)
def __str__(self):
return (self.Complex ' Settings')
Forms.py:
class SettingsForm(ModelForm):
class Meta:
model = SettingsClass
fields = ('Complex','Trial_balance_Year_to_date' , 'Trial_balance_Monthly' , 'Income_Statement_Year_to_date' , 'Income_Statement_Monthly' , 'Age_Analysis' , 'Balance_Sheet' , 'Repair_and_Maintenance_General_Ledger' , 'Major_capital_Items_General_Ledger')
class SettingUpdateForm(ModelForm):
class Meta:
model = SettingsClass
fields = '__all__'
ViewSettings.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">
{% block content %}
{{ error }}
<h2 style="text-align: center">{{ setting.Complex }}</h2>
<hr>
<br>
<form method="POST">
{% csrf_token %}
<div class="container">
<ul class='list-group'>
<li class='list-group-item active'>{{ form.Trial_balance_Year_to_date }} Trial Balance Year To Date</li>
<li class='list-group-item'>{{ form.tbytd_Include_opening_balances }} Include Opening Balances</li>
<li class='list-group-item'>{{ form.tbytd_Only_use_main_accounts }} Only Use Main Accounts</li>
<li class='list-group-item'>{{ form.tbytd_Print_null_values }} Print 0.00 Values</li>
<li class='list-group-item'>{{ form.tbytd_Print_description }} Print Description</li>
<li class='list-group-item'>{{ form.tbytd_Print_account }} Print Account</li>
<li class='list-group-item'>{{ form.tbytd_Sort_by_account_name }} Sort By Account Name</li>
</ul>
<br>
<ul class='list-group'>
<li class='list-group-item active'>{{ form.Trial_balance_Monthly }} Trial Balance Monthly</li>
<li class='list-group-item'>{{ form.tbm_Only_use_main_accounts }} Only Use Main Accounts</li>
<li class='list-group-item'>{{ form.tbm_Print_null_values }} Print 0.00 Values</li>
<li class='list-group-item'>{{ form.tbm_Print_description }} Print Description</li>
<li class='list-group-item'>{{ form.tbm_Print_account }} Print Account</li>
<li class='list-group-item'>{{ form.tbm_Sort_by_account_name }} Sort By Account Name</li>
</ul>
<br>
<ul class='list-group'>
<li class='list-group-item active'>{{ form.Income_Statement_Monthly }} Income Statement Monthly</li>
<li class='list-group-item'>{{ form.ism_Only_use_main_accounts }} Only Use Main Accounts</li>
<li class='list-group-item'>{{ form.ism_Print_null_values }} Print 0.00 Values</li>
<li class='list-group-item'>{{ form.ism_Print_description }} Print Description</li>
<li class='list-group-item'>{{ form.ism_Print_account }} Print Account</li>
<li class='list-group-item'>{{ form.ism_Sort_by_account_name }} Sort By Account Name</li>
</ul>
<br>
<ul class='list-group'>
<li class='list-group-item active'>{{ form.Income_Statement_Year_to_date }} Income Statement Year To Date</li>
<li class='list-group-item'>{{ form.isytd_Only_use_main_accounts }} Only Use Main Accounts</li>
<li class='list-group-item'>{{ form.isytd_Print_null_values }} Print 0.00 Values</li>
<li class='list-group-item'>{{ form.isytd_Print_description }} Print Description</li>
<li class='list-group-item'>{{ form.isytd_Print_account }} Print Account</li>
<li class='list-group-item'>{{ form.isytd_Sort_by_account_name }} Sort By Account Name</li>
</ul>
<br>
<ul class='list-group'>
<li class='list-group-item active'>{{ form.Age_Analysis }} Age Analysis</li>
<li class='list-group-item'>{{ form.aa_Include_all_customers }} Include All Customers</li>
<li class='list-group-item'>{{ form.aa_include_null_values }} Print 0.00 Values</li>
<li class='list-group-item'>{{ form.aa_include_on_hold_customers }} Include On Hold Customers</li>
<li class='list-group-item'>{{ form.aa_Include_cash_customers }} Include Cash Customers</li>
<li class='list-group-item'>{{ form.aa_allocate_unalocated_credits_to_oldest }} Allocate Unalocated Credits To Oldest Records</li>
</ul>
<br>
<ul class='list-group'>
<li class='list-group-item active'>{{ form.Balance_Sheet }} Balance Sheet</li>
<li class='list-group-item'>{{ form.bs_Only_use_main_accounts }} Only Use Main Accounts</li>
<li class='list-group-item'>{{ form.bs_Print_current_month }} Print The Current Month (Otherwise prints year to date)</li>
<li class='list-group-item'>{{ form.bs_Include_null_values }} Include 0.00 values</li>
<li class='list-group-item'>{{ form.bs_Print_assets_first }} Print Assets First</li>
<li class='list-group-item'>{{ form.bs_statement_of_financial_position }} Statement Of Financial Position</li>
</ul>
<br>
<ul class='list-group'>
<li class='list-group-item active'>{{ form.Repair_and_Maintenance_General_Ledger }} Repair And Maintenance General Ledger</li>
<li class='list-group-item'>{{ form.rmgl_Ignore_inactive_accounts }} Ignore Inactive Accounts</li>
<li class='list-group-item'>{{ form.rmgl_Sort_by_transaction_date }} Sort By Transaction Date</li>
<li class='list-group-item'>{{ form.rmgl_Group_by_account_type }} Group By Account Type</li>
<li class='list-group-item'>{{ form.rmgl_Print_account }} Print Account</li>
<li class='list-group-item'>{{ form.rmgl_Print_year_to_date }} Print Year To Date (Otherwise prints for the current month)</li>
</ul>
<br>
<ul class='list-group'>
<li class='list-group-item active'>{{ form.Major_capital_Items_General_Ledger }} Repair And Maintenance General Ledger</li>
<li class='list-group-item'>{{ form.mcigl_Ignore_inactive_accounts }} Ignore Inactive Accounts</li>
<li class='list-group-item'>{{ form.mcigl_Sort_by_transaction_date }} Sort By Transaction Date</li>
<li class='list-group-item'>{{ form.mcigl_Group_by_account_type }} Group By Account Type</li>
<li class='list-group-item'>{{ form.mcigl_Print_account }} Print Account</li>
<li class='list-group-item'>{{ form.mcigl_Print_year_to_date }} Print Year To Date (Otherwise prints for the current month)</li>
</ul>
</div>
<br>
<br>
<hr>
<div class="btn-update" style="padding-left: 1%">
<button type="submit" class='btn btn-primary'>Update</button>
<button type="delete" class="btn btn-danger">Delete</button>
</div>
</form>
{% endblock %}
CodePudding user response:
yes, because your form doesnt submit to the URL of your update view, since you have a settings objects which u need to update and you are passing the settings_pk in the view
so you need to add id of the settings which you need to update.
change this line in the HTML
<form method="POST">
to
<form method="POST" action="{ url 'view_name' settings.pk }">