I have tried couple of ways to avoid this error from view code and on django template as well. Also tried to popped out the fields as well but none are working and getting the same error every time(This field is required) . We are using step wizards to show and hide the fields on different form wizard steps from views.py . Whenever we visit the previous page on click on previous button then only the step 2 fields are empty and showing the error. Appreciate any quick help on this.
Here is the models.py
from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
class CStructure(models.Model):
z_caisson_lower_limit = models.FloatField(verbose_name='C level limits', default=5.0)
z_caisson_upper_limit = models.FloatField(verbose_name='C limits', default=10.0)
z_caisson_step_size = models.FloatField(verbose_name='C level limits', default=1)
caisson_width_lower_limit = models.FloatField(verbose_name='C width limits', default=10)
caisson_width_upper_limit = models.FloatField(verbose_name='C width limits', default=20)
caisson_width_step_size = models.FloatField(verbose_name='C width limits', default=5)
Here is the form.html
{% block content %}
{% if wizard.steps.current == '3' %}
<div id="loading-overlay">
<i class="fas fa-spin fa-8x fa-cog" id="loading-icon"></i>
<p>
Calculation can be cancelled on the <a target="_blank" href="{% url
'project_detail' pk=project_id %}">project
page</a>.
</p>
</div>
{% endif %}
// Below are the steps of wizards to show the fields or fields label
<div class="container-fluid">`enter code here`
<div class="row form-steps">
<div class="form-step col-md-2 offset-md-1 {% if wizard.steps.current == '0' %}active{% endif %}">Design
& Hydraulic
</div>
<div class="form-step col-md-2 {% if wizard.steps.current == '1' %}active{% endif %}">Shape constraints
</div>
<div class="form-step col-md-2 {% if wizard.steps.current == '2' %}active{% endif %}">Material</div>
<div class="form-step col-md-2 {% if wizard.steps.current == '3' %}active{% endif %}">Cost unit rates</div>
<div class="form-step col-md-2 {% if wizard.steps.current == '4' %}active{% endif %}">Results</div>
</div>
</div>
<div class="container">
<div id=input_container" class="row">
<div class="col-sm-12 pt-5 pb-5">
{% bootstrap_messages %}
{% block bootstrap4_content %}
{% load i18n %}
{{ form }}
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger">
<strong>{{ field.label }}</strong>: {{ error|escape }}
</div>
{% endfor %}
{% endfor %}
{% endif %}
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ wizard.form.media }}
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{% bootstrap_form form layout='horizontal' %}
{% endfor %}
{% endif %}
Here is the form.py
class CaissonStructureStep(forms.ModelForm):
class Meta:
model = CaissonStructure
fields = ['z_caisson_lower_limit',
'z_caisson_upper_limit', 'z_caisson_step_size',
'caisson_width_lower_limit', 'caisson_width_upper_limit']
widgets = {
'z_caisson_lower_limit': forms.TextInput(attrs={'class': 'cs form-control'}),
'z_caisson_upper_limit': forms.TextInput(attrs={'class': 'cs form-control'}),
'z_caisson_step_size': forms.TextInput(attrs={'class': 'cs form-control'}),
'caisson_width_lower_limit': forms.TextInput(attrs={'class': 'cs form-control'}),
'caisson_width_upper_limit': forms.TextInput(attrs={'class': 'cs form-control'}),
'caisson_width_step_size': forms.TextInput(attrs={'class': 'cs form-control'}),
'z_base_lower_limit': forms.TextInput(attrs={'class': 'cs form-control'}),
'z_base_upper_limit': forms.TextInput(attrs={'class': 'cs form-control'}),
'z_base_step_size': forms.TextInput(attrs={'class': 'cs form-control'})
}
def clean(self):
cleaned_data = super(CaissonStructureStep2, self).clean()
for field in list(cleaned_data):
if '_lower_limit' in field:
custom_validator.validate_min_max_step_size(self, cleaned_data, field)
return cleaned_data
def __init__(self, request, *args, **kwargs):
super(CaissonStructureStep2, self).__init__(*args, **kwargs)
Here is the code in view.py to hide form fields
from django import forms
def get_form(self, step=None, data=None, files=None):
form = super(WaterBreakerWizard, self).get_form(step, data, files)
step = step or self.steps.current
if step == '1':
caisson_type_elements = self.storage.data['step_data']['0']['0-caisson_type']
if ('CAISSON' in caisson_type_elements):
fields_to_remove = ['z_c_lower_limit', 'z_c_upper_limit', 'z_c_step_size']
for fields in fields_to_remove:
# form.fields.pop(fields)
form.fields[fields].widget = forms.HiddenInput()
if ( 'RUBBLEMOUND' in caisson_type_elements):
fields_to_remove = ['z_caisson_lower_limit', 'z_caisson_upper_limit']
for fields in fields_to_remove:
# form.fields.pop(fields)
form.fields[fields].widget = forms.HiddenInput()
elif step == '2':
caisson_type_elements = self.storage.data['step_data']['0']['0-caisson_type']
if ('RUBBLEMOUND' in caisson_type_elements):
fields_to_remove = ['rho_caisson', 'rho_fd', 'rho_fw', 'rho_bd', 'rho_bw']
for fields in fields_to_remove:
if fields in form.fields:
form.fields[fields].widget = forms.HiddenInput()
if ('CAISSON' in caisson_type_elements):
fields_to_remove = ['rho_w', 'rho_c', 'rho_r', 'rock_gradings',
'core_material']
for fields in fields_to_remove:
# form.fields.pop(fields)
form.fields[fields].widget = forms.HiddenInput()
return form