For the past few days, I have been trying to find a solution to this issue on Stack Overflow. Click here for reference.
By following the advice provided in this discussion about preventing empty form submissions (link here), I am very close to resolving my problem.
The main challenge lies in how I'm implementing a django DetailView and customizing the GET method to retrieve a value passed by Javascript. Unfortunately, it seems to be causing a loop whenever I execute my GET request. Given my limited experience with Javascript, there's a possibility that my script contains errors. Below is a snippet of my code...
This is my Django FormView...
class AuthorByNameView(LoginRequiredMixin,FormView):
form_class = AuthorByNameForm
template_name = 'author_by_name.html'
def get_form_kwargs(self):
kwargs = super(AuthorByNameView, self).get_form_kwargs()
kwargs['dropdown'] = self.request.GET.get("dropdown")
return kwargs
And my Django DetailView...
class AuthorDetailView(LoginRequiredMixin,DetailView):
model = Author
template_name = 'author_detail.html'
def get_object(self, queryset=None):
return get_object_or_404(Author, id=self.request.GET.get("dropdown"))
def get(self, request, *args, **kwargs):
dropdown=self.request.GET.get("dropdown")
if dropdown is not None:
if Author.objects.filter(Q(id=self.request.GET.get("dropdown"))).distinct():
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
else:
print(dropdown)
messages.add_message(self.request, messages.INFO, 'Please enter a valid request number.')
return HttpResponseRedirect(reverse('Main:author_detail'))
My HTML snippet...
<form method="GET" autocomplete=off action="{% url 'Main:author_detail' %}">
This is my FORM...
class AssociateByNameForm(forms.Form):
dropdown = forms.ModelChoiceField(queryset=User.objects.none(),required=False)
def __init__(self, *args, **kwargs):
dropdown = kwargs.pop('dropdown', None)
super(AssociateByNameForm, self).__init__(*args, **kwargs)
self.fields['dropdown'].widget.attrs['class'] = 'choices'
self.fields['dropdown'].empty_label = ''
self.fields['dropdown'].queryset = Author.objects.filter(is_active=True).order_by('last_name','first_name')
self.fields['dropdown'].label_from_instance = lambda obj: "%s" % obj.get_full_name()
Here's my Javascript snippet...
$(document).ready(function () {
$('form').submit(function() {
var dropdown = $('#id_dropdown').val();
if (dropdown === undefined || dropdown === "") {
$('#id_dropdown').attr('name', '' );
}
});
});
Based on my observations from print statements...Whenever the detailview is triggered, Javascript keeps sending the value "NONE" repeatedly to the GET request. This floods the Python backend with error messages defined in my GET method.
Is there a way to fix this issue so that the GET method only receives the value "NONE" once? My goal is to display an error message through the DetailView if a user submits the form without entering any value. The same code works fine in other instances, but when using a ModelChoiceField and manipulating the value in case of a blank entry, issues arise. I tried sending "NONE" to avoid an invalid literal BASE10 error.
I am open to alternative approaches, but it appears that preventing multiple occurrences of "NONE" being sent via JavaScript to the GET method might resolve the problem. Perhaps the JavaScript code lacks a return statement? I'm just speculating, as I'm relatively new to Javascript.
Thank you in advance for any insights or suggestions.