Trying to fetch a selected approver and validate before submitting a form in Django has proven quite complex. I've made significant progress, but I'm stuck on figuring out how to "get" the translated ID in the Django template. I have the ID, but I can't make the final connection.
Here's the AJAX snippet:
$("#avail").on("click", function(event) {
var modalavail = document.getElementById("myModalavail");
var markedCheckbox = document.querySelectorAll('input[type="checkbox"]:checked');
event.preventDefault();
$(window).unbind('beforeunload');
$.ajax({
method: 'get',
headers: { "X-CSRFToken": token },
url: "",
processData: false,
contentType: false,
cache: false,
enctype: 'multipart/form-data',
success: function(resp) {
modalavail.style.display = "block";
for (var checkbox of markedCheckbox) {
console.log(checkbox.value + ' ');
}
},
error: function (request, status, error) {
console.log(request.responseText);
showAjaxFormErrors(JSON.parse(request.responseText));
$('html, body').animate({ scrollTop: 0 }, 50);
},
});
});
Here's the HTML snippet:
Check Availability
<div class="table56">
{% for entry in form.approvers %}
<table class="table57">
<thead>
<tr>
<th class="title156">Name</th>
<th class="title118">Available?</th>
</tr>
</thead>
<tbody>
<tr>
<td class="title156">{{ entry.name }}</td>
{% if not start_conflict or end_conflict or during_conflict %}
<td class="title117">Yes</td>
{% else %}
<td class="title117">No</td>
{% endif %}
</tr>
</tbody>
</table>
{% endfor %}
</div>
<input type="hidden" id="modal-approvers">
<button type="button" class="button210" id="modalcloseavail">Close</button>
</div>
Here's the form snippet:
class DailyPlannerForm(forms.ModelForm):
class Meta:
model = NewDailyPlannerEvent
exclude = [ 'created_by','summary']
def __init__(self, user, *args, **kwargs):
start_time = kwargs.pop('start_time', None)
User = get_user_model()
super(DailyPlannerForm, self).__init__(*args, **kwargs)
self.fields['approvers'] = forms.ModelMultipleChoiceField(
widget=forms.CheckboxSelectMultiple(),
queryset=User.objects.exclude(Q(is_active=False)).order_by('last_name','first_name'),required=False)
def clean(self):
cleaned_data = super(DailyPlannerForm, self).clean()
approvers = cleaned_data.get('approvers')
Here's the model snippet:
class DailyPlannerEvent(models.Model):
STATUS_CHOICES = (
("AwaitingResponse","AwaitingResponse"),
("Cancelled","Cancelled"),
("Declined","Declined"),
("Delete","Delete"),
("Saved","Saved"),
("Scheduled","Scheduled"),
("Submitted","Submitted"),
)
STATUS_CHOICES1 = (
("Cancel","Cancel"),
)
STATUS_CHOICES3 = (
("True","True"),
("False","False"),
("N/A","N/A")
)
approver_by_department = models.ManyToManyField(Department,related_name="new_daily_planner_event_approver_by_department")
approvers = models.ManyToManyField(User)
created_by = models.ForeignKey(User,null=True,on_delete=models.CASCADE,related_name='new_daily_planner_event_created_by')
creation_date = models.DateTimeField(editable=False,null=True)
daily_planner_event_creation_date = models.DateTimeField(editable=False,null=True)
daily_planner_event_name = models.CharField(max_length=255,blank=True)
end_time = models.DateTimeField(null=True,blank=True)
last_update = models.DateTimeField(null=True)
request_type = models.CharField(choices=REQUEST_TYPE,default="New Daily Planner Event Request",max_length=35)
start_time = models.DateTimeField(null=True,blank=True)
status = models.CharField(choices=STATUS_CHOICES,default="Submitted",max_length=20)
submitted = models.CharField(choices=STATUS_CHOICES3,default="False",null=True,blank=True,max_length=20)
And for the view snippet:
def get_context_data(self, **kwargs):
context = super(DailyPlannerEventView, self).get_context_data(**kwargs)
start_conflict = DailyPlannerEvent.objects.filter(
start_time__range=(dailyplannerevent.start_time,
dailyplannerevent.end_time,approver=user))
end_conflict = DailyPlannerEvent.objects.filter(
end_time__range=(dailyplannerevent.start_time,
dailyplannerevent.end_time,approver=user))
during_conflict = DailyPlannerEvent.objects.filter(
start_date__lte=dailyplannerevent.start_time,
end_date__gte=dailyplannerevent.end_time,approver=user)
context['start_conflict'] = start_conflict
context['end_conflict'] = end_conflict
context['during_conflict'] = during_conflict
return context
Struggling with how to handle this line in the HTML:
{% for entry in form.approvers %}
Trying to get only the selected approvers in the form to display. Any insights are greatly appreciated. Thanks!