Why is this django form not valid? - python

Why is this form not validating? It is not even calling the clean() method.
forms.py:
class SingleSampleForm(forms.Form):
sample_id = forms.CharField(label='Sample ID:')
class Meta:
fields = ('sample_id',)
def __init__(self, *args, **kwargs):
super(SingleSampleForm, self).__init__()
self.helper = FormHelper()
self.helper.layout = Layout(
Field('sample_id',
css_class="search-form-label",),
Submit('submit', 'Search sample', css_class='upload-btn')
)
self.helper.form_method = 'POST'
def clean(self):
print('CLEAN')
sample_id = self.cleaned_data['sample_id']
if sample_id:
return sample_id
raise ValidationError('This field is required')
views.py:
class SampleView(View):
sample_form = SingleSampleForm
def get(self, request, *args, **kwargs):
sample_form = self.sample_form()
self.context = {'sample_form': sample_form,}
return render(request,
'results/single_sample_search.html',
self.context)
def post(self, request, *args, **kwargs):
self.sample_form = self.sample_form(request.POST)
if self.sample_form.is_valid():
print('Valid')
else:
print('not valid')
self.context = {
'sample_form': self.sample_form,
}
return render(request,
'results/single_sample_search.html',
self.context)
I don't understand why it is not even calling the clean() method. I have another form which is almost identical which validates. When I do print dir(self.sample_form) after I have passed the request.POST dict it states that validation=unknown. Why is this? How do I check the reason it is not validating?

You need to pass *args and **kwargs when you call super():
def __init__(self, *args, **kwargs):
super(SingleSampleForm, self).__init__(*args, **kwargs)
At the moment, calling __init__ without any *args or **kwargs is equivalent to calling with data=None. The form is unbound, so will never be valid.

Related

Pass kwarg into an inlineformset_factory?

I am trying to pass the request object into my inlineformset_factory and am struggling to accomplish this.
In forms.py I have the following:
class SummativeScoreForm(forms.ModelForm):
"""
Form definition for SummativeScore Form
"""
subdomain_proficiency_level = forms.ModelChoiceField(
empty_label="Undecided",
queryset=SubdomainProficiencyLevel.objects.none(),
widget=forms.RadioSelect,
required=False,
)
def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request", None)
super(SummativeScoreForm, self).__init__(*args, **kwargs)
if self.instance:
if self.request.user == self.instance.summative.employee:
self.fields["subdomain_proficiency_level"].disabled = True
self.fields[
"subdomain_proficiency_level"
].queryset = SubdomainProficiencyLevel.objects.filter(
subdomain=self.instance.subdomain
)
self.fields[
"subdomain_proficiency_level"
].label = f"""
{self.instance.subdomain.character_code}:
{self.instance.subdomain.short_description}
"""
class Meta:
model = SummativeScore
fields = "__all__"
widgets = {
"subdomain_proficiency_level": forms.RadioSelect(
attrs={"class": "list-unstyled"}
),
}
SummativeScoreInlineFormset = inlineformset_factory(
Summative,
SummativeScore,
fields=("subdomain_proficiency_level",),
can_delete=False,
extra=0,
form=SummativeScoreForm,
)
I'm using a FormView CBV to show this inline_formset
class SummativeScoreFormView(
LoginRequiredMixin,
UserIsObserverOrObserveeMixin,
SingleObjectMixin,
FormView,
):
model = Summative
template_name = "commonground/summative_score_form.html"
pk_url_kwarg = "summative_id"
def get(self, request, *args, **kwargs):
summative_id = kwargs.pop("summative_id")
self.object = self.get_object(
queryset=Summative.objects.filter(id=summative_id)
)
return super().get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
summative_id = kwargs.pop("summative_id")
self.object = self.get_object(
queryset=Summative.objects.filter(id=summative_id)
)
return super().get(request, *args, **kwargs)
def get_form(self, form_class=None):
return SummativeScoreInlineFormset(
**self.get_form_kwargs(), instance=self.object
)
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["request"] = self.request
return kwargs
def form_valid(self, form):
form.save()
messages.add_message(messages.SUCCESS, "Changes were saved!")
HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form):
print("invalid form")
return super().form_invalid(form)
def get_success_url(self):
user_id = self.kwargs["user_id"]
summative_id = self.kwargs["summative_id"]
return reverse(
"commonground:summative_detail",
kwargs={
"user_id": user_id,
"summative_id": summative_id,
},
)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
summative = get_object_or_404(
Summative, pk=self.kwargs["summative_id"]
)
context["summative"] = summative
return context
I keep getting this error:
__init__() got an unexpected keyword argument 'request'
I'm not sure how best to resolve this - any ideas? Is there a clear way to pass the request to the inlineformset_factory?
You are passing the request to the formset and not to the forms in the formset, hence you get the error. For passing custom parameters to the formsets forms [Django docs] you need to pass a form_kwargs parameter to the formset:
class SummativeScoreFormView(
LoginRequiredMixin,
UserIsObserverOrObserveeMixin,
SingleObjectMixin,
FormView,
):
...
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['form_kwargs'] = {'request': self.request}
return kwargs

Validate django form with a list of values from multiple CheckboxSelectMultiple

Using Django2.0, I have set up a form which displays all of my model objects in a list with check boxes. When this is submitted, in my request dict it stores them as a list of IDs. How do i validate these within the form? The clean() method does not get called.
forms:
class SampleRunSearchForm(forms.ModelForm):
sample_run_id = forms.ModelChoiceField(
label='Sample Run',
queryset=SampleRun.objects.all(),
widget=forms.CheckboxSelectMultiple,
)
class Meta:
model = SampleRun
fields = ('sample_run_id',)
def __init__(self, *args, **kwargs):
super(SampleRunSearchForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
Field('sample_run_id', css_class='sample-run-display',),
HTML('<br>'),
Submit('submit', 'Report samples', css_class='upload-btn')
)
self.helper.form_method = 'GET'
def clean_sample_run_id(self):
sr_id = self.cleaned_data.getlist('sample_run_id')
for sr in sr_id:
... do something to validate...
else:
raise(forms.ValidationError('Error'))
return(sr_id)
views:
class SearchSampleRun(View):
samplerunform = SampleRunSearchForm
template_name = 'results/samplerun_search_form.html'
def get(self, request, *args, **kwargs):
samplerunform = self.samplerunform()
if request.GET:
samplerunform = self.samplerunform(request.GET)
samplerunform.is_valid()
context = {'samplerunform': samplerunform}
return render(request, self.template_name, context)
It returns an error to my page that is it not a valid choice.
the clean() method will also execute, but clean_sample_run_id() doesn't work - do i need to iterate through each ID and pass it through the form seperately for validation?!

Filter Django ModelForm without validating it

I have a form which I would like to filter based on information passed by another form, but without validating it just yet:
forms.py:
class SampleRunSearchForm(forms.ModelForm):
class Meta:
model = SampleRun
fields = ('id',)
def __init__(self, sr_obj, *args, **kwargs):
super(SampleRunSearchForm, self).__init__(*args, **kwargs)
self.fields['id'] = forms.ChoiceField(required=True,
label='Sample:',
widget=forms.CheckboxSelectMultiple,
choices=((s.id, s) for s in sr_obj)
)
self.helper = FormHelper()
self.helper.layout = Layout(
Field('id', css_class='sample-run-display',),
Submit('submit', 'Report samples', css_class='upload-btn')
)
self.helper.form_method = 'POST'
views.py:
class SearchSampleRun(View):
samplerunform = SampleRunSearchForm
template_name = 'results/samplerun_search_form.html'
def get(self, request, *args, **kwargs):
self.run_obj = get_object_or_404(Run, id=kwargs['run_id'])
self.choice = kwargs['choice']
self.sample_run_obj = self.obtainCorrectSamples()
samplerunform = self.samplerunform(sr_obj=self.sample_run_obj)
context = {'samplerunform': samplerunform}
return render(request, self.template_name, context)
def post(self, request, *args, **kwargs):
samplerunform = self.samplerunform(request.POST)
if samplerunform.is_valid():
HttpResponseRedirect(...somewhere to display information)
context = {}
return render(request, self.template_name, context)
The initial form (not shown) takes a charfield and redirects to my SearchSampleRun view with **kwargs. I want to filter my SampleRunSearchForm based on these kwargs and display a list of check boxes - filtered model object from the SampleRun model. This works, but when i click these buttons, and submit the form, it initialised again, and sr_obj is None, so the form field produces an error.
I have tried using:
sr_obj = kwargs.pop('sr_obj', None)
In my init() method, but these must be a way to dynamically filter a form queryset in order to display a subset of values, before validating, with a view to validating when this form is submitted?
Just add validation to the __init__ method and override id fields only if sr_objis not empty:
def __init__(self, sr_obj, *args, **kwargs):
super(SampleRunSearchForm, self).__init__(*args, **kwargs)
if sr_obj:
self.fields['id'] = forms.ChoiceField(required=True,
label='Sample:',
widget=forms.CheckboxSelectMultiple,
choices=((s.id, s) for s in sr_obj)
)
self.helper = FormHelper()
self.helper.layout = Layout(
Field('id', css_class='sample-run-display',),
Submit('submit', 'Report samples', css_class='upload-btn')
)
self.helper.form_method = 'POST'

Passing request object from view to form in Django

I'm trying to create an account edit page which visually contains a single form (ie single submit button) but the fields are part of two (or more) different models. I've pieced together a solution from several SO answers and the form loads fine on GET requests but I would now like to conditionally hide/display the terms of service checkbox field based on what url is being accessed. Specifically on registration the TOS should be displayed while it should not on the account edit page. Simplified code looks like so:
# views.py
class _RequestPassingFormView(FormView):
http_method_names = ['get', 'post', 'head', 'options', 'trace']
def get(self, request, *args, **kwargs):
# Pass request to get_form_class and get_form for per-request
# form control.
form_class = self.get_form_class(request)
form = self.get_form(form_class)
return self.render_to_response(self.get_context_data(form=form))
def post(self, request, *args, **kwargs):
# Pass request to get_form_class and get_form for per-request
# form control.
form_class = self.get_form_class(request)
form = self.get_form(form_class)
if form.is_valid():
# Pass request to form_valid.
return self.form_valid(request, form)
else:
return self.form_invalid(form)
def get_form_class(self, request=None):
return super(_RequestPassingFormView, self).get_form_class()
def get_form_kwargs(self, request=None, form_class=None):
return super(_RequestPassingFormView, self).get_form_kwargs()
def get_initial(self, request=None):
return super(_RequestPassingFormView, self).get_initial()
def get_success_url(self, request=None, user=None):
# We need to be able to use the request and the new user when
# constructing success_url.
return super(_RequestPassingFormView, self).get_success_url()
def form_valid(self, form, request=None):
return super(_RequestPassingFormView, self).form_valid(form)
def form_invalid(self, form, request=None):
return super(_RequestPassingFormView, self).form_invalid(form)
class AccountEditView(_RequestPassingFormView):
form_class = AccountEditForm
template_name = 'account_edit.html'
def form_valid(self, request, form):
success_url = self.get_success_url(request, new_user)
try:
to, args, kwargs = success_url
return redirect(to, *args, **kwargs)
except ValueError:
return redirect(success_url)
def get_success_url(selfself,request, user):
return '/account'
#forms.py
class CombinedFormBase(forms.Form):
form_classes = []
def __init__(self, *args, **kwargs):
super(CombinedFormBase, self).__init__(*args, **kwargs)
for f in self.form_classes:
name = f.__name__.lower()
setattr(self, name, f(*args, **kwargs))
form = getattr(self, name)
self.fields.update(form.fields)
self.initial.update(form.initial)
def is_valid(self):
isValid = True
for f in self.form_classes:
name = f.__name__.lower()
form = getattr(self, name)
if not form.is_valid():
isValid = False
# is_valid will trigger clean method
# so it should be called after all other forms is_valid are called
# otherwise clean_data will be empty
if not super(CombinedFormBase, self).is_valid() :
isValid = False
for f in self.form_classes:
name = f.__name__.lower()
form = getattr(self, name)
self.errors.update(form.errors)
return isValid
def clean(self):
cleaned_data = super(CombinedFormBase, self).clean()
for f in self.form_classes:
name = f.__name__.lower()
form = getattr(self, name)
cleaned_data.update(form.cleaned_data)
return cleaned_data
class RegistrationForm(forms.Form):
required_css_class = 'required'
email = forms.EmailField(label=_('E-mail'))
password1 = forms.CharField(widget=forms.PasswordInput,
label=_('Password'))
password2 = forms.CharField(widget=forms.PasswordInput,
label=_('Password (again)'))
"""
Conditionlly display TOS checkbox based on context
"""
def __init__(self, *args, **kwargs):
"""
add in a field for terms of service here if viewing
the registration form
"""
super(RegistrationForm, self).__init__(*args, **kwargs)
class AccountProfileForm(forms.Form):
required_css_class = 'required'
company = forms.CharField(label=('Company Name'))
class AccountEditForm(CombinedFormBase):
form_classes = [RegistrationForm, AccountProfileForm]
This is my first django project so it's possible that this is a completely wrong direction also. If so a hint towards a simpler solution would be appreciated.
As you are using class based view, I think you can try like this:
view:
class _RequestPassingFormView(FormView):
http_method_names = ['get', 'post', 'head', 'options', 'trace']
def get(self, request, *args, **kwargs):
form_class = self.get_form_class()
self.object = None
form = form_class(request_data=request)
return self.render_to_response(self.get_context_data(form=form))
Or like this:
class _RequestPassingFormView(FormView):
http_method_names = ['get', 'post', 'head', 'options', 'trace']
#No need to override the get method in this view.
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs.update(request_data=self.request)
return kwargs
And form is like this:
class RegistrationForm(forms.Form):
#form fields
def __init__(self, *args, request_data=None, **kwargs):
super().__init__(*args, **kwargs)
print(request_data)
# do other operations
I'm gonna keep it super simple. If you need further details, please refer here:
def my_view(request):
if request.method == 'POST':
# Sending your request info as kwarg.
form = myform(request.POST, user=request.user)
class MyForm(forms.Form):
def __init__(self, *args, **kwargs):
# Recieving it.
self.user = (kwargs.pop('user', None))
super(MyForm, self).__init__(*args, **kwargs)
You may want to consider just having two separate forms. If the only difference is the one on the registration page has a terms of service checkbox, then that form class could inherit from the other class and have the extra form field.

Dynamically populating choicefield in Django

I can't initialize the Choicefield form inside the views.py. I tried passing the option variable in the __init__ function but I got an error:
__init__() takes at least 2 arguments (1 given)` coming from the `form = super(SomeeWizard, self).get_form(step, data, files)
forms.py
class SomeForm(forms.Form):
def __init__(self, choice, *args, **kwargs):
super(SomeForm, self).__init__(*args, **kwargs)
self.fields['choices'] = forms.ChoiceField(choices=[ (o.id, str(o)) for o in choice])
views.py
class SomeWizard(SessionWizardView):
def get_form(self, step=None, data=None, files=None):
form = super(SomeWizard, self).get_form(step, data, files)
if step == "step2":
option = self.get_cleaned_data_for_step("step1")['choice']
choice = Choice.objects.filter(question__text__exact=option)
form = SomeForm(choice)
return form
def get_template_names(self):
return [TEMPLATES[self.steps.current]]
def done(self, form_list, **kargs):
return render_to_response('done.html')
EDIT
I tried Hasan solution and Django Form Wizard is passing {'files': None, 'prefix': 'step2', 'initial': {}, 'data': None} into the **kwarg in the __init__ function of SomeForm.
I printed the content in the **kwarg and I got:
{'files': None, 'prefix': 'step2', 'initial': {}, 'data': None}
{'choices': [<Choice: Blue>, <Choice: Red>]}
try this change(I comment changed line):
forms.py:
class SomeForm(forms.Form):
def __init__(self, *args, **kwargs): #this line changed
choice = kwargs.pop('choice', None) #this line added
self.fields['choices'] = forms.ChoiceField(choices=[ (o.id, str(o)) for o in choice])
super(SomeForm, self).__init__(*args, **kwargs)
views.py:
class SomeWizard(SessionWizardView):
def get_form(self, step=None, data=None, files=None):
form = super(SomeWizard, self).get_form(step, data, files)
if step == "step2":
option = self.get_cleaned_data_for_step("step1")['choice']
choice = Choice.objects.filter(question__text__exact=option)
form = SomeForm(choice=choice) #this line changed
return form
def get_template_names(self):
return [TEMPLATES[self.steps.current]]
def done(self, form_list, **kargs):
return render_to_response('done.html')
FYI I had to init the form with the data attribute for this to work. For example:
class SomeWizard(SessionWizardView):
def get_form(self, step=None, data=None, files=None):
form = super(SomeWizard, self).get_form(step, data, files)
# determine the step if not given
if step is None:
step = self.steps.current
if step == "2":
option = self.get_cleaned_data_for_step("1")['choice']
choice = Choice.objects.filter(question__text__exact=option)
## Pass the data when initing the form, which is the POST
## data if the got_form function called during a post
## or the self.storage.get_step_data(form_key) if the form wizard
## is validating this form again in the render_done methodform
form = SomeForm(choice=choice, data=data)
return form
def get_template_names(self):
return [TEMPLATES[self.steps.current]]
def done(self, form_list, **kargs):
return render_to_response('done.html')
Otherwise when the form was submitted it just returned my back to this first form. For a full explanation see here. I am using django 1.8 though.

Categories