I've spent the last couple days trying to figure out the answer to this SO...ModelChoiceField invalid literal for int() with base 10: ''
Leveraging this solution, How to prevent submitting the HTML form's input field value if it empty I'm really close.
The problem is...I'm using a django DetailView and overriding GET to try and get the value that is being passed by Javascript, but it's essentially looping when I perform my GET. I'm new to Javascript, and it's entirely possible that my Javascript program is incorrect. Here is my code....
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
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....
<form method="GET" autocomplete=off action="{% url 'Main:author_detail' %}">
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()
My Javascript....
$(document).ready(function () {
$('form').submit(function() {
var dropdown = $('#id_dropdown').val();
if (dropdown === undefined || dropdown === "") {
$('#id_dropdown').attr('name', '' );
}
});
});
From what I can tell via my print statements....When the detailview is called, the Javascript is sending the value of NONE recursively to the GET request. The Python backend is then overrun by the error messages I've defined in my GET method.
Is there some way to correct this so that the GET method only gets the value of NONE one time? I'm trying to return an error message via my DetailView in the event that a user clicks submit on the FORM. I am using this same code in many other places and it works just fine. The difference is that in this case I'm using a ModelChoiceField and having to manipulate the value if the user clicks on the form and the value is blank. I am trying to send a value of NONE to prevent an invalid literal BASE10 message.
I'm open to other approaches, but it would seem that if I can just prevent the request.GET.get from receiving or the Javascript from sending NONE numerous times, this code would do exactly what I want. I suspect maybe the Javascript code is maybe missing a return? Just a guess on my part as I'm very new to Javasript.
Thanks in advance for any thoughts.