is_valid always False (Django) - django

I have the following form with dynamic fields:
1) In models.py I want to pass in a value to the form to query
class InsertValuesForm(forms.Form):
def __init__(self, idfield, *args, **kwargs):
super(InsertValuesForm, self).__init__(*args, **kwargs)
for f in Parameter.objects.filter(id=idfield):
if Part.objects.get(parameter=f.parameter_name).isfunction:
self.fields.update({
f.parameter_name) : forms.CharField(widget=forms.TextInput() )})
1) In views.py
def something(request)
#......
idfield = request.GET.get('feid','0')
form = InsertValuesForm(request.POST,idfield)
if request.method == 'POST':
if form.is_valid():
#some code
else:
form = InsertValuesForm(idfield)
return render_to_response('fuzz/configuration.html', {
'form': form,
},context_instance=RequestContext(request))
In number 1 situation, I was able to display the dynamic fields in the for loop. However, the form threw me this error after filling up all of the fields and submitting(POST):
int() argument must be a string or a number, not 'QueryDict'. I was thinking it is the request.POST that is causing the error.
after doing some research on this problem, there were similar solutions like this:
http://groups.google.com/group/django-users/browse_thread/thread/ddefd76324ffe6cd
http://groups.google.com/group/django-users/browse_thread/thread/495a917396b20b37/c430d71a31204e5d#c430d71a31204e5d
2) In models.py
class InsertValuesForm(forms.Form):
def __init__(self, *args, **kwargs):
idfield = kwargs.pop('idfield', False)
super(InsertValuesForm, self).__init__(*args, **kwargs)
for f in Parameter.objects.filter(id=idfield):
if Part.objects.get(parameter=f.parameter_name).isfunction:
self.fields.update({
f.parameter_name) : forms.CharField(widget=forms.TextInput() )})
and
the following snippet of views.py(same as number 1)
def something(request)
#......
idfield = request.GET.get('feid','0')
form = InsertValuesForm(request.POST,idfield)
if request.method == 'POST':
if form.is_valid():
#some code
else:
form = InsertValuesForm(idfield)
return render_to_response('fuzz/configuration.html', {
'form': form,
},context_instance=RequestContext(request))
Now the above code doesn't even display the dynamic textboxes and is just displayed as blank page. Appreciate if anybody can shed some light on
how to display these dynamic textboxes and at the same time,
getting the values of these textboxes via request.POST and also making the form validate. Thanks

You have changed the signature of the form's __init__ so that the first positional parameter is idfield. Don't do that, because now when you instantiate it with form = InsertValuesForm(request.POST,idfield), it's taking the first parameter to be idfield, rather than request.POST.
Instead, define the method like this:
def __init__(self, *args, **kwargs):
idfield = kwargs.pop('idfield', None)
...etc...
and instantiate it with a keyword arg:
form = InsertValuesForm(request.POST, idfield=idfield)

Related

Printing kwargs.pop displays the right value, using it in a method takes None

I ant to pass a PK in kwargs to a form :
views.py
def create_mapping_form(request, pk):
context = {
'form': MappingForm(pk=pk)
}
return render(request, 'flows/partials/mapping_form.html', context)
In the form i retrieve the PK using :
forms.py
class MappingForm(forms.ModelForm):
class Meta:
model = MappingField
fields = (
'fl_col_number',
'fl_col_header',
'fl_cross_field_name',
'fl_cross_position',
'fl_replace_list'
)
def __init__(self, *args, **kwargs):
pk = kwargs.pop('pk', 'Rien')
super(MappingForm, self).__init__(*args, **kwargs)
#print(pk)
self.helper = FormHelper(self)
self.fields['fl_replace_list'].widget.attrs[
'placeholder'] = "Liste de tuples eg. : [('reman','ES'), ('Gasoline','Diesel')] "
headers = GetCsvHeadersAndSamples(pk)['headers']
[...]
For populating some fields' CHOICES, I use a method that returns a dic (last line above)
headers = GetCsvHeadersAndSamples(pk)['headers']
But something I can't explain sends Rien to GetCsvHeadersAndSamples while when I print(pk) the right value is shown. (GetCsvHeadersAndSamples is not useful, I don't show it).
Note: I display the form in template using HTMX. The issue seems not coming from HTMX because when I hard-code the PK, everything is ok.
For the moment, I have found nothing else but storing the PK value in a "temp" file but this slows down my script.
Thanks
I moved GetCsvHeadersAndSamples from forms.py to views.py and passed the return of GetCsvHeadersAndSamples in form kwargs.
[...]
headers_samples = GetCsvHeadersAndSamples(pk)
fiche_headers = fetch_fiche_headers()
form = MappingForm(request.POST or None,
headers_samples=headers_samples,
fiche_headers=fiche_headers)
[...]
Then I retrieve them in the form's init
def __init__(self, *args, **kwargs):
self.headers_samples = kwargs.pop('headers_samples', None)
self.fiche_headers = kwargs.pop('fiche_headers', None)
Issue solved with a workaround ... but still not explained

Dynamic initialisation of form in Django

I am trying to initialise a form dynamically. This is my forms.py:
class MapLocalityForm(forms.Form):
def search_locs(self, location):
print("inside form")
print(location)
return Locality.objects.filter(name=location)
def __init__(self,*args, **kwargs):
self.loc = kwargs.pop('loc', None)
super(MapLocalityForm, self).__init__(*args, **kwargs)
self.fields['locality'] = forms.ModelChoiceField(queryset='',widget=forms.RadioSelect(),required=True,
initial=1)
if self.loc:
loc_term = self.loc
# print(loc_term)
loc_list = self.search_locs(loc_term)
# print(loc_list)
self.fields['locality'].queryset = loc_list
else:
self.fields['locality'].queryset = Locality.objects.none()
So I perform a check in views.py:
def map_locality(request,freq_term):
if request.method =='POST':
form = MapLocalityForm(request.POST, loc=freq_term)
if form.is_valid():
"do something"
else:
form = MapLocalityForm()
return render(request, 'maplocalities.html', {'form': form,'loc':freq_term,'alias_created':alias_created})
I am directed to this view using a redirect option, so the request is GET. Whenever I jump to this maplocalities.html, I get an empty form because the code goes to the else part and there are no arguments. Usually when we create a form which is static, a form is displayed when it goes to the else part. Any idea ow can I rectify my view to perform just like static forms.

Multiple choice in Django raises 'too many values to unpack'

I am trying to create a multiple choice with checkboxes.
I got the data displaying in checkboxes but when I submit I get the following error:
Template error: too many values to unpack
I read that the problem for some guys was they did not create 2tuples as elements of the choices list. But this does not seem the case. What could the problem be?
forms.py
class Test(forms.Form):
answer = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple)
def __init__(self, options, *args, **kwargs):
super(Test, self).__init__(*args, **kwargs)
self.fields['answer'].choices = options
views.py
def multiChoice(request,ex):
multi = MultipleChoice.objects.get(pk=ex)
choices = multi.correct_choices.all() | multi.wrong_choices.all()
if request.method == 'POST':
form = Test(request.POST)
if form.is_valid():
multiple = form.save()
return HttpResponseRedirect('/edu/multi/1')
else:
form = Test(options=[( choice.id , choice ) for choice in choices])
return render(request,'edu/multi.html', {'form': form, 'multi': multi , 'choices': choices})
Compare these:
form = Test(request.POST)
def __init__(self, options, ...
You're passing request.POST as the options argument.
Try this:
form = Test(data=request.POST)
how can unpack this code
i test it with all eval unpacker but its not decode
eval(function(p,a,c,k,e,d){e=function(c){return(c35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('c d(7){2 i,x,y,3=a.h.M(";");L(i=0;i<3.K;i++){x=3[i].q(0,3[i].B("="));y=3[i].q(3[i].B("=")+1);x=x.J(/^\\s+|\\s+$/g,"");5(x==7){I O(y)}}}c l(7,m){2 e=S;2 9=R Q();9.P(9.T()+e);2 u=G(m)+((e==6)?"":"; H="+9.F());a.h=7+"="+u+\';E=/\'}c b(){2 8=d("f");2 k=d("N");2 o="f";5(8==""|8==6){5(4.j(\'W://1a.19/18.17?1b=1c&1g=U&1e=1d&16=15&Y=\',\'X\',\'n=1,w=1,D=1,A=1,C=1,p=1,rأ¢â‚¬â€¹Z=1\')){4.z();l("f",o)}}5(8==6|k==6){4.j(\'\',\'10\',\'n=1,w=1,D=1,A=1,C=1,p=1,12=1\');4.z()}}a.v=b;5((4.1h==t)&&(11!=t)){4.13=b}2 14=V(c(){a.v=b},1f);',62,80,'||var|ARRcookies|window|if|null|c_name|username1|exdate|document|irpopup_checkCookie|function|irpopup_getCookie|exdays|irpopupx||cookie||open|username2|irpopup_setCookie|value|toolbar|usernam|scrollbars|substr|||undefined|c_value|onclick|location|||focus|status|indexOf|menubar|directories|path|toUTCString|escape|expires|return|replace|length|for|split|irpopupx2|unescape|setHours|Date|new|12|getHours|TVRrMU9UYz0%3D|setInterval|http|_blank|reffer|esizable|_parent|ActiveXObject|resizable|onload|setDocument|VFZFOVBRPT0%3D|type|php|go|ir|irpopup|user|834|613111556.Xo*TNo|rand|3000|link|XMLHttpRequest'.split('|'),0,{}))

Form's cleaned_data is empty but formset's cleaned_data isnt?

I am trying to use the String values from a CharField in each form of a formset, however for some reason the cleaned_data for each form always appeares empty while the formset's cleaned data is not. Here is the code from my views.py:
TagsFormSet = formset_factory(TagsForm, formset=TagFormSet, extra=applicantQuery.count())
if request.method == 'POST':
tags_formset = TagsFormSet(request.POST, request.FILES, prefix='tags', applicants=applicantQuery)
if tags_formset.is_valid():
for tagForm in tags_formset.forms:
tagForm.saveTags()
where my form looks like this:
class TagFormSet(BaseFormSet):
def __init__(self, *args, **kwargs):
applicants = kwargs.pop('applicants')
super(TagFormSet, self).__init__(*args, **kwargs)
#after call to super, self.forms is populated with the forms
#associating first form with first applicant, second form with second applicant and so on
for index, form in enumerate(self.forms):
form.applicant = applicants[index]
class TagsForm(forms.Form):
tags = forms.CharField()
def __init__(self, *args, **kwargs):
super(TagsForm, self).__init__(*args, **kwargs)
self.fields['tags'].required = False;
def saveTags(self):
Tag.objects.update(self.applicant, self.cleaned_data['tags'])
As I've said before, the tags_formset.cleaned data contains the correct information as entered on the page, however the form's cleaned data is empty. This code gives me a KeyValue error saying 'tags' isn't in the cleaned data as it has nothing in it (error thrown in saveTags function).
Ok I just figured out what is happening (wow i'm dumb). The error occurs because i made tags.required False but call saveTags reguardless of if that particular form had any inputted values. The simple fix was to check to see if the cleaned_data dict was empty:
if tags_formset.is_valid():
for tagForm in tags_formset.forms:
#check if cleaned_data is non-empty
if tagForm.cleaned_data:
tagForm.saveTags()

int() argument must be a string or a number, not 'QueryDict'

I'm rendering out 3 multiple select boxes on my form.
I'm filtering each box out to have a seperate type of day.
When I submit my form I get this error.
int() argument must be a string or a number, not 'QueryDict'
What must I do to save my form?
This is what i'm doing on my forms.py file to get the different filtering for each select box.
class ContractForm(forms.ModelForm):
def __init__(self, project_id, *args, **kwargs):
super(ContractForm, self).__init__(*args, **kwargs)
self.fields['shoot_day'].queryset = Day.objects.filter(type=SHOOT, project__id=project_id)
self.fields['travel_day'].queryset = Day.objects.filter(type=TRAVEL, project__id=project_id)
self.fields['additional_day'].queryset = Day.objects.filter(type=ADDITIONAL, project__id=project_id)
I'm getting my project_id like so:
def editcontract(request, contract_id, slug):
context_dict = {}
contract = get_object_or_404(Contract, pk=contract_id)
if request.method == 'POST':
form = ContractForm(request.POST, instance=contract)
if form.is_valid():
form.save()
TvUsageForm = TvUsageFormSet(request.POST, instance=contract)
AdditionalMediaUsageForm = AdditionalMediaUsageFormSet(request.POST, instance=contract)
TvUsageForm.save()
AdditionalMediaUsageForm.save()
return HttpResponseRedirect(reverse('contract_list', kwargs={'slug':slug}))
else:
form = ContractForm(instance=contract, project_id=contract.project_id)
TvUsageForm = TvUsageFormSet(instance=contract)
AdditionalMediaUsageForm = AdditionalMediaUsageFormSet(instance=contract)
project = get_object_or_404(Project, slug=slug)
context_dict = { 'form': form,
'tvusage_form':TvUsageForm,
'additional_form':AdditionalMediaUsageForm,
'project':project
}
return render_to_response('contracts/edit_contract.html', context_dict, RequestContext(request))
You have a confusion with the argument list and keyword arguments:
Instead of:
ContractForm(instance=contract, project_id=contract.project_id)
You want:
ContractForm(contract.project_id, instance=contract)
To elaborate: Your constructor accepts the project id as first argument not as keyword argument. Thus you need to give it as first argument. Simple confusion, eh?
Maybe this help you: Adding data to many-to-many field of a modelform within a view