Sorry I'm new to django, I need to transfer a list by POST in django, The problem is that the list elements need to be complex objects, dictionaries every list entry should have first name and last name parameters.
I only know how to make list of simple values like this :
<input type="hidden" name="alist" value="1" />
<input type="hidden" name="alist" value="2" />
<input type="hidden" name="alist" value="3" />
and then :
alist = request.POST.getlist('alist')
What is the best practice of doing this?
Thanks
It's not at all clear what you want to do, but perhaps you could your list as JSON and post that.
Related
I'm working on an application with Struts 1 and JSP. I have to write XSS protection. I have inputs like this one :
<input id="name" name="name" class="someClass" type="text"
value="<bean:write name="personForm" property="name"/>">
I read that for protection XSS attack i have to add attribute filter in bean:write and filter should be true. So my code looks like that now
<input id="name" name="name" class="someClass" type="text"
value="<bean:write name="personForm" property="name" filter="true"/>">
But still I'm able to submit scripts. Do you know why this might happen.
bean:write is only for rendering purposes.The value passed to the server side is not get filtered.
I am creating a form with cfloop and need to access each variable individually when submitting the form. I need to use the selected entries of the form in a loop that will add my selections to a database.
This is my form:
<form method="post">
<input type="hidden" name="isPost" value="1">
...
<cfoutput>
<cfloop query="client_admin_surveys">
<input type="text" size="35" name="surveyID" id="surveyID" value="#id#">
<input type="text" size="35" name="surveyName" id="surveyName" value="#name#">
<input type="checkbox" name="amplify" id="amplify">
<input type="checkbox" name="enchance" id="enchance">
<input type="checkbox" name="pacify" id="pacify">
<input type="checkbox" name="pacifyUrgent" id="pacifyUrgent">
</cfloop>
</cfoutput>
...
<input type="submit" name="submit" value="Submit">
</form>
After posting the form, the results group all of my selections because I have the same "name" for my form elements. I tried adding an i count next to each name to make it different but then I got a bit confused about how to process the fields.
You started down the correct path when you added the counter - go back and add that, something like:
<input type="checkbox" name="amplify#client_admin_surveys.currentRow#" id="amplify">
Would work.
I also sometimes like to add a form field for the 'counter' on the processing page
<input type="hidden" name="counter" value="#client_admin_surveys.recordCount#" />
Then on the processing page, you can loop over the counter and access the form fields using bracket notation
<cfloop from="1" to="#form.counter#" indexd="i">
<cfset thisAmplify = form["amplify" & i] />
<cfset thisEnhance = form["enhance" & i] />
<!---- more logic here --->
</cfloop>
In coldfusion, in the hidden values of a form I get two values:
<input type="hidden" name="EST_VAL" value="#EST_VAL#" />
<input type="hidden" name="DOWN_PMT" value="#DOWN_PMT#" />
I need to find the value of EST_VAL subtracted from DOWN_PMT and assign it to this value:
<input type="hidden" name="LOAN_VAL" value="#LOAN_VAL#" />
I know how to do this in PHP, but I'm not sure if I should use a cfset, or cffunction in coldfusion.
you can do this inline with pound signs like so:
<input type="hidden" name="LOAN_VAL" value="#EST_VAL-DOWN_PMT#"/>
CFSET should be all you need for this. Without seeing the other code in this file/script, a simple approach/solution would entail the following:
<cfset LOAN_VAL = EST_VAL - DOWN_PMT />
That would then output the value of the LOAN_VAL variable in the hidden field tag you note in your original post.
Suppose the parameter I want to pass is called printdata, and printdata=['a', 'b', 'c'].
I use "input type="hidden" name="alist" value={{printdata}}>" to pass
the parameter. However, when I try to retrieve the parameter in
views.py using the following code:
params = request.POST
params["alist"] is equal to ['a', instead of ['a', 'b', 'c']. I suspect that
django identifies the end of a parameters using ,.
Do you have any suggestions or any other methods to pass the parameter?
Use multiple hidden fields, one for each value, in your HTML:
<input type="hidden" name="alist" value="1" />
<input type="hidden" name="alist" value="2" />
<input type="hidden" name="alist" value="3" />
On the server side, use request.POST.getlist to read all the values as a list:
alist = request.POST.getlist('alist')
# 'alist' now contains ['1', '2', '3']
Make sure to put quotation marks around the list string in your HTML. ie:
<input type="hidden" name="alist" value="{{printdata}}">
Use
request.POST.getlist('alist')
to get the values.
alist = request.POST.get('alist')
I have an edit object view that contains a formset(one or many if this matters), now I want to create a page that can display multiple edit object forms and submit it in a single form.
What is the correct way to achieve this task?
I found a solution.
I can enumerate my objects on edit page and use different prefixes for formsets based on these indexes. Here is an example:
First, you need enumeration, I achieved it using same input(checkbox) name with incremental values:
<input type="checkbox" name="counter" value="0">
...
<input type="checkbox" name="counter" value="1">
...
Counter numbers is the formset and other data serial numbers:
<!--Ordinary inputs-->
<input type="text" name="data0" value="value0">
<input type="text" name="data1" value="value1">
<!--Formsets-->
<input type="text" id="test0-0-data" name="test0-0-data" value="something">
<input type="text" id="test0-1-data" name="test0-1-data" value="something">
<input type="hidden" name="test0-TOTAL_FORMS" id="id_test0-TOTAL_FORMS" value="2">
<input type="hidden" name="test0-INITIAL_FORMS" id="id_test0-INITIAL_FORMS" value="0">
<input type="text" id="test1-0-data" name="test1-0-data" value="something">
<input type="hidden" name="test1-TOTAL_FORMS" id="id_test1-TOTAL_FORMS" value="1">
<input type="hidden" name="test1-INITIAL_FORMS" id="id_test1-INITIAL_FORMS" value="0">
Then if code you populate formsets like this:
counter = request.POST.getlist('counter')
for i in counter:
TestFormset = modelformset_factory(Test, form=TestForm)
test_formset = TestFormset(request.POST, prefix='test'+i, queryset=Test.objects.none())
I achieved HTML structure above with JavaScript.