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
Related
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
I'm trying to override my form to show a select with my data, my problem is when I try to send the form I get this error init() got multiple values for argument.
form.py
class ChangeMemberForm(forms.Form):
coordinator = forms.ModelChoiceField(queryset=None,label="Pesquisadores", widget=forms.Select(attrs={'class':'form-control'}))
def __init__(self,ubc, *args,**kwargs):
super(ChangeMemberForm, self).__init__(*args,**kwargs)
ubc = Ubc.objects.get(id=ubc)
self.fields['coordinator'].queryset = ubc.researchers.all()
view.py
def change_coordinator(request, pk):
template_name = 'ubc/addmember.html'
ubc = Ubc.objects.get(pk=pk)
if request.method == "POST":
form = ChangeMemberForm(request.POST, ubc=ubc.id)
if form.is_valid():
print(form.cleaned_data)
return redirect('accounts:account_detail', pk=request.user.id)
form = ChangeMemberForm(ubc=ubc.id)
context = {
'form': form,
}
return render(request, template_name, context)
The order in which you defined the parameters is:
def __init__(self, ubc, *args, **kwargs):
whereas you make a call to the __init__ with:
form = ChangeMemberForm(request.POST, ubc=ubc.id)
So you called it with an unnamed first parameter, whereas the first parameter is ubc. So as a result, the __init__ function received two values for the ubc parameter: request.POST, and ubc.id, and of course that does not make sense.
So you should replace it with:
form = ChangeMemberForm(ubc.id, request.POST)
So here that means that the first parameter (ubc.id) is redirected to the ubc parameter, and the second (request.POST) is the first parameter of the *args, which is a parameter of the super().__init__ call.
The convention in django is to pass the form data as the first positional argument when you initialize a form.
The problem with your ChangeMemberForm is that ubc is declared as the first positional argument. So when you try to ChangeMemberForm(request.POST, ubc=ubc.id) both arguments would be interpreted as ubc, and python throws an exception.
When you subclass Form it's better to use only named arguments for any extra arguments required by your custom form class. With python 3, simply put ubc between *args and **kwargs in the __init__ method signature.
class ChangeMemberForm(forms.Form):
def __init__(self, *args, ubc, **kwargs):
...
(If you are using python 3.4 or earlier, you must include a default value for ubc, as mentioned by Willem in a comment)
If you have to support python 2, this syntax is not valid, and you must instead do something like this.
class ChangeMemberForm(forms.Form):
def __init__(self, *args, **kwargs):
ubc = kwargs.pop('ubc')
...
I am newbie with Django and I get stucked trying to pass the value from a html table rendered with django-tables2 to a form.
view.py
def configView(request):
form = ConfigForm(request.POST or none)
if form.is_valid():
save_it = form.save(commit=False)
save_it.save()
Messages.success(request, 'Configuracion Actualizada')
return HttpResponseRedirect('/monitor/')
return render_to_response("config.html",
locals(),
context_instance=RequestContext(request))
This is my forms.py
class ConfigForm(forms.ModelForm):
class Meta:
model = Config
def __init__(self, *args, **kwargs):
super(ConfigForm, self).__init__(*args,**kwargs)
self.fields['id_proveedor'].initial = kwargs.pop('id_proveedor',None)
But I don't know how to retrieve and pass the value to theform.
I need pass the values from the cells 0, 2 and 6.
Any advice or snippet will be appreciated.
Thanks in advance
I would try this:
class ConfigForm(forms.Form):
def __init__(self, *args, **kwargs):
your_variable_to_pass = kwargs.pop("your_variable_to_pass")
super(ConfigForm, self).__init__(*args,**kwargs)
self.fields['id_proveedor']= forms.FieldClass(attribute=your_variable_to_pass)
id_proveedor = FieldClass()
where, 'FieldClass' is whatever field you choose (i.e. ChoiceField, CharField) and
attribute is the attribute to pass (your variable), i.e. 'choices', 'initial' etc.
thus, it may look like this:
self.fields['id_proveedor']= forms.ChoiceField(choices=your_variable_to_pass)
id_proveedor = ChoiceField()
Notice indentation - you assign value of the attribute to pass in the constructor!; in case of ChoiceField choices is a list of tuples, i.e. (('1', 'First',), ('2', 'Second',)); I use Forms instead of ModelForm as super or base class in this example
Then, in the views: f = ConfigFrom(request.POST, your_variable_to_pass=your_variable_to_pass)
notice your_variable_to_pass=your_variable_to_pass otherwise it'll generate a key error
I hope, it helps!
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,{}))
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)