Hi everyone,
My django app is using a form and storing the data in a DB.
this is my Form
softwareServer = forms.ModelChoiceField(
queryset=Servers.objects.filter(serverType="LINUX"),
label="Choose the Linux server for software server",
required=False,
)
softwareClient = forms.ModelChoiceField(
queryset=Servers.objects.filter(serverType="WINDOWS"),
label="Choose the Windows server for software client",
required=False,
)
this is my java script on the form page
const isClientServerCombinedCheckbox = document.querySelector('input[name="isClientServerCombined"]');
const softwareLocationSelect = document.querySelector('select[name="softwareLocation"]');
const softwareServerSelect = document.querySelector('select[name="softwareServer"]');
const softwareClientSelect = document.querySelector('select[name="softwareClient"]');
function resetAndDisableSelect(selectElement, disable = true) {
selectElement.innerHTML = '<option value="">Select...</option>';
selectElement.disabled = disable;
}
function fetchServersAndUpdateSelect(serverType, locationId, selectElement) {
resetAndDisableSelect(selectElement); // Disable and reset the select element
if (!locationId) return; // Exit if no locationId is provided
fetch(`/ajax/get_servers/?server_type=${serverType}&location_id=${locationId}`)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json(); // This returns a promise
})
.then(data => {
console.log(data);
resetAndDisableSelect(selectElement, false);
data.forEach(server => {
const option = new Option(server.serverName, server.id);
selectElement.add(option);
});
if (isClientServerCombinedCheckbox.checked && serverType === 'LINUX') {
selectElement.disabled = true;
}
})
.catch(error => {
console.error('Error:', error);
resetAndDisableSelect(selectElement);
});
}
softwareLocationSelect.addEventListener('change', () => {
const locationId = softwareLocationSelect.value;
fetchServersAndUpdateSelect('LINUX', locationId, softwareServerSelect);
fetchServersAndUpdateSelect('WINDOWS', locationId, softwareClientSelect);
});
isClientServerCombinedCheckbox.addEventListener('change', function() {
const locationId = softwareLocationSelect.value;
if (this.checked) {
softwareServerSelect.disabled = true;
softwareServerSelect.value = '';
if (locationId) {
fetchServersAndUpdateSelect('WINDOWS', locationId, softwareClientSelect);
}
} else {
softwareServerSelect.disabled = false;
if (locationId) {
fetchServersAndUpdateSelect('LINUX', locationId, softwareServerSelect);
}
}
});
// Initial setup call
if (softwareLocationSelect.value) {
softwareLocationSelect.dispatchEvent(new Event('change'));
}
if (isClientServerCombinedCheckbox.checked) {
softwareServerSelect.disabled = true;
}
const dynamicFieldsContainer = document.querySelector('#dynamicFields');
this is the view
@login_required
def create_tform(request):
if request.method == "POST":
form = TForm(request.POST)
if form.is_valid():
app = form.save()
call_command('export_properties')
context = {
"software_detail_url": request.build_absolute_uri(f"/software_detail/{trade_app.id}"),
}
html_content = render_to_string("email_template.html", context)
text_content = strip_tags(html_content)
subject = "New Form has submitted!"
from_email = "t@bl.com"
selected_mailing_lists = form.cleaned_data["mailing_lists"]
subscribers = Subscriber.objects.filter(mailing_list__in=selected_mailing_lists).distinct()
to_emails = subscribers.values_list("email", flat=True)
if to_emails:
email = EmailMultiAlternatives(subject, text_content, from_email, to=to_emails)
email.attach_alternative(html_content, "text/html")
email.send(fail_silently=False)
return HttpResponseRedirect(f"/software_detail/{trade_app.id}")
else:
form = TForm()
return render(request, "create_software.html", {"form": form, "WORKER_GROUPS": WORKER_GROUPS})
after i hit submit, all the selection of this specific two (software server and software client) disappears and i cant pick any and cant submit with values
what could lead to this problem? i cant update this values at all
Thanks!