Django forms + list of checkboxes + list of radiobuttons - django

Do You know how to change this list of checkboxes code:
<p>How did you reach this site? <select name="howreach">
<option value="0" selected="selected">Choose one...</option>
<option value="1">Typed the URL directly</option>
<option value="2">Site is bookmarked</option>
<option value="3">A search engine</option>
<option value="4">A link from another site</option>
<option value="5">From a book</option>
<option value="6">Other</option>
</select></p>
to the django forms?
And how to change this to the list of radio buttons in Django forms?:
Poor <input type="radio" name="rating" value="1" /> 1
<input type="radio" name="rating" value="2" /> 2
<input type="radio" name="rating" value="3" /> 3
<input type="radio" name="rating" value="4" /> 4
<input type="radio" name="rating" value="5" /> 5 Excellent</p>

In your python code:
class SiteReach(forms.Form):
howreach = forms.ChoiceField(label = "How did you reach this site?",
choices = HOWREACH_CHOICES,
widget = forms.widgets.CheckboxInput())
You'll have to initialize HOWREACH_CHOICES on your own; it's a list of tuples, (option value, option string).
You render radio buttons in the same way:
class Rating(forms.Form):
rating = forms.ChoiceField(choices = range(1,6),
widget = forms.widgets.RadioSelect())
Read the documentation on Widgets; there's a whole universe of utility in there.

Related

Get values for Radio/Checkboxes outputted to email?

Heres my code:
<cfparam name='form.firstName' Default=''>
<cfparam name='form.yes' Default=''>
<cfparam name='form.no' Default=''>
<cfparam name='form.clean' Default=''>
<cfparam name='form.light' Default=''>
<cfparam name='form.heavy' Default=''>
<cfparam name='form.superheavy' Default=''>
<label for="firstName">Guest Access:</label>
<input type="textbox" name="firstName" value="#form.firstName#">
<label for="allkeysaccountedfor">All keys are accounted for?:</label>
<select>
<option value="#form.yes#" name="yes">Yes</option>
<option value="#form.no#" name="no">No</option>
</select>
<label for="unitcondition">Unit Condition?:</label>
<input type="radio" name="clean" value="#form.clean#"><span>Clean</span>
<input type="radio" name="light" value="#form.light#"><span>Light</span>
<input type="radio" name="heavy" value="#form.heavy#"><span>Heavy</span>
<input type="radio" name="superheavy" value="#form.superheavy#"><span>SuperHeavy</span>
When I submit the form, I get an email returning what I put into the text box, but when I check yes or no or select an option for the radio button, nothing gets returned in the email. Any advice would be greatly appreciated thanks!
You're setting name attributes in your options for the select and radio buttons. I would instead try setting it to the parent element like this:
<cfparam name='form.firstName' default=''>
<cfparam name='form.allkeysaccountedfor' default=''>
<cfparam name='form.unitcondition' default=''>
<label for="firstName">Guest Access:</label>
<input type="textbox" id="firstName" name="firstName" value="#form.firstName#">
<label for="allkeysaccountedfor">All keys are accounted for?:</label>
<select name="allkeysaccountedfor" id="allkeysaccountedfor">
<option value="yes" <cfif allkeysaccountedfor eq "yes">selected</cfif>>Yes</option>
<option value="no" [[do the same for no here]] >No</option>
</select>
<label for="unitcondition" id="unitcondition">Unit Condition?:</label>
<input type="radio" name="unitcondition" value="clean" <cfif unitcondition eq "clean">checked</cfif>><span>Clean</span>
<input type="radio" name="unitcondition" value="light" [[do the same for this value]] ><span>Light</span>
<input type="radio" name="unitcondition" value="heavy" [[do the same for this value]] ><span>Heavy</span>
<input type="radio" name="unitcondition" value="superheavy" [[do the same for this value]] ><span>SuperHeavy</span>
Then, in your email, I would look for the 3 form variables above. Also note that I added "id's" to your HTML elements. That's because the "for" attribute in the label specifies which form element a label is bound to and uses the "id" attribute to do that.

Transfer Value from drop down to a

I need some help ...
I need the location drop down box to determine the appropriate inbox the form should be sent to.
ex if I choose Houston from the drop down box it will send it to the PayrollUSA email.
Im currently using a radio button to make the selection but i would like to automate with the drop down.
Im pretty new to this but im sure theres a if statement that can tie them together...
im using .asp for this.
<input type="radio" name="payroll" value="PayrollUSA#mail.com" checked="checked">US Payroll
<input type="radio" name="payroll" value="PayrollCAN#mail.com">CAN Payroll
<input type="radio" name="payroll" value="PayrollUK#mail.com">UK Payroll
<input type="radio" name="payroll" value="PayrollHK#mail.com">HK Payroll
Drop down selection
<SELECT SIZE="1" NAME="Business_Unit" style="width: 205px;" class="answers">
<option selected >Select</option>
<OPTION>Calgary</OPTION>
<OPTION>Chicago</OPTION>
<OPTION>Hong Kong</OPTION>
<OPTION>Houston</OPTION>
<OPTION>London</OPTION>
<OPTION>Los Angeles</OPTION>
<OPTION>Montreal</OPTION>
<OPTION>New York</OPTION>
<OPTION>New York Corporate</OPTION>
<OPTION>Philadelphia</OPTION>
<OPTION>San Francisco</OPTION>
<OPTION>Toronto</OPTION>
<OPTION>Toronto Corporate</OPTION>
<OPTION>Vancouver</OPTION>
</SELECT>
If you need the option value to be held then try this:
<SELECT SIZE="1" NAME="Business_Unit" style="width: 205px;" class="answers">
<option value="">Select an Option</option>
<option <% if Business_Unit= "PayrollCAN#mail.com" then %> selected <% End if %> value="PayrollCAN#mail.com">Calgary</option>
<option <% if Business_Unit= "PayrollUSA#mail.com" then %> selected <% End if %> value="PayrollUSA#mail.com">Chicago</option>
</select>
etc. for each option and same value may be used for different option displays
I imaging this should just be html based. So your option menu should look like:
<SELECT SIZE="1" NAME="Business_Unit" style="width: 205px;" class="answers">
<option value="-1">Select an Option</option>
<OPTION value="PayrollCAN#mail.com">Calgary</OPTION>
<OPTION value="PayrollUSA#mail.com">Chicago</OPTION>
<OPTION value="PayrollHK#mail.com">Hong Kong</OPTION>
<OPTION value="PayrollUSA#mail.com">Houston</OPTION>
<OPTION value="PayrollUK#mail.com">London</OPTION>
<OPTION value="PayrollUSA#mail.com">Los Angeles</OPTION>
<OPTION value="PayrollCAN#mail.com">Montreal</OPTION>
<OPTION value="PayrollUSA#mail.com">New York</OPTION>
<OPTION value="PayrollUSA#mail.com">New York Corporate</OPTION>
<OPTION value="PayrollUSA#mail.com">Philadelphia</OPTION>
<OPTION value="PayrollUSA#mail.com">San Francisco</OPTION>
<OPTION value="PayrollCAN#mail.com">Toronto</OPTION>
<OPTION value="PayrollCAN#mail.com">Toronto Corporate</OPTION>
<OPTION value="PayrollCAN#mail.com">Vancouver</OPTION>
</SELECT>
You can use the same value multiple times if needed

ColdFusion not adding variable to url

Currently I'm in the works of creating a refrral system for my website, but somehow ColdFusion won't let me add a value to an URL.
When people sign up, they use a referral url which looks like this: /register/?referer=[name]. Followed by the following form:
<cfif IsDefined('URL.referer')>
<cfquery name="CheckRefer" datasource = "#DSN#">
SELECT username, ip_reg, look
FROM users
WHERE username='<cfoutput>#referer#</cfoutput>'
</cfquery> </cfif>
<form id="register_step_two" method="post" action="complete.cfm<cfif #referer# is not 0 and #CheckRefer.RecordCount# is 1 and #CGI.REMOTE_ADDR# is not #CheckRefer.ip_reg#>?referer=<cfoutput>#referer#</cfoutput></cfif>">
<input type="hidden" name="referer" id="referer" value="#referer#" />
<input type="hidden" name="email" id="email" value="#form.email#" />
<input type="hidden" name="bdday" id="bdday" value="#form.bdday#" />
<input type="hidden" name="bdmonth" id="bdmonth" value="#form.bdmonth#" />
<input type="hidden" name="bdyear" id="bdyear" value="#form.bdyear#">
<input type="hidden" name="username" id="username" value="" />
<input type="hidden" name="figure" id="figure" value="hr-834-61.hd-600-4.ch-3014-110.lg-3006-110-62.sh-3089-110.ca-3217-62-62,s-0.g-0.d-4.h-4.a-0,c201c1c1b7fceffd8d5e8f5204f593ef" />
<input type="hidden" name="password" id="password" value="#form.password#" />
<input type="submit" value="Registreer" id="register-button" onmousedown="this.style.backgroundColor='##ddd';" onmouseup="this.style.backgroundColor='##eee';" onmouseover="this.style.backgroundColor='##eee';" onmouseout="this.style.backgroundColor='##fff';" disabled="disabled" />
</form>
But after clicking the submit button it redirects me to complete.cfm and not complete.cfm?referer=[name]. I don't know why this happens, because I'm using the same method on other pages. All the variables inside the are defined...
For example: this form works perfectly fine (the code posted above is page 2 of 3 from my signup process, and this one underneed is the first one, page 3 is the complete.cfm).
<cfif IsDefined('URL.referer')>
<cfquery name="CheckRefer" datasource = "#DSN#">
SELECT username, ip_reg, look
FROM users
WHERE username='<cfoutput>#referer#</cfoutput>'
</cfquery> </cfif>
<form id="register_step_one" method="post" action="step2.cfm<cfif IsDefined('URL.referer') and URL.referer is not 0 and #CheckRefer.RecordCount# is 1 and #CGI.REMOTE_ADDR# is not #CheckRefer.ip_reg#>?referer=<cfoutput>#referer#</cfoutput></cfif>">
<label for="email">Email</label><br />
<input type="text" name="email" id="formemail" />
<div class="errormsg display_none" id="habbo_name_message_box">
<h3>Eoutmelding</h3>
Something went wrong..
</div>
<br /><br />
<label for="password">New password</label><br />
<input type="password" name="password" id="formpassword" />
<br /><br>
<label for="cpassword">Repeat password<label><br />
<input type="password" name="cpassword" id="formcpassword" />
<br /><br />
Date of birth:<br />
<select name="bdday" id="bdday" class="dateselector"><option value="">Dag</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option><option value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option value="31">31</option></select> <select name="bdmonth" id="bdmonth" class="dateselector"><option value="">Maand</option><option value="1">Januari</option><option value="2">Februari</option><option value="3">Maart</option><option value="4">April</option><option value="5">Mei</option><option value="6">Juni</option><option value="7">Juli</option><option value="8">Augustus</option><option value="9">September</option><option value="10">Oktober</option><option value="11">November</option><option value="12">December</option></select> <select name="bdyear" id="bdyear" class="dateselector"><option value="">Jaar</option><option value="2010">2010</option><option value="2009">2009</option><option value="2008">2008</option><option value="2007">2007</option><option value="2006">2006</option><option value="2005">2005</option><option value="2004">2004</option><option value="2003">2003</option><option value="2002">2002</option><option value="2001">2001</option><option value="2000">2000</option><option value="1999">1999</option><option value="1998">1998</option><option value="1997">1997</option><option value="1996">1996</option><option value="1995">1995</option><option value="1994">1994</option><option value="1993">1993</option><option value="1992">1992</option><option value="1991">1991</option><option value="1990">1990</option><option value="1989">1989</option><option value="1988">1988</option><option value="1987">1987</option><option value="1986">1986</option><option value="1985">1985</option><option value="1984">1984</option><option value="1983">1983</option><option value="1982">1982</option><option value="1981">1981</option><option value="1980">1980</option><option value="1979">1979</option><option value="1978">1978</option><option value="1977">1977</option><option value="1976">1976</option><option value="1975">1975</option><option value="1974">1974</option><option value="1973">1973</option><option value="1972">1972</option><option value="1971">1971</option><option value="1970">1970</option><option value="1969">1969</option><option value="1968">1968</option><option value="1967">1967</option><option value="1966">1966</option><option value="1965">1965</option><option value="1964">1964</option><option value="1963">1963</option><option value="1962">1962</option><option value="1961">1961</option><option value="1960">1960</option><option value="1959">1959</option><option value="1958">1958</option><option value="1957">1957</option><option value="1956">1956</option><option value="1955">1955</option><option value="1954">1954</option><option value="1953">1953</option><option value="1952">1952</option><option value="1951">1951</option><option value="1950">1950</option><option value="1949">1949</option><option value="1948">1948</option><option value="1947">1947</option><option value="1946">1946</option><option value="1945">1945</option><option value="1944">1944</option><option value="1943">1943</option><option value="1942">1942</option><option value="1941">1941</option><option value="1940">1940</option><option value="1939">1939</option><option value="1938">1938</option><option value="1937">1937</option><option value="1936">1936</option><option value="1935">1935</option><option value="1934">1934</option><option value="1933">1933</option><option value="1932">1932</option><option value="1931">1931</option><option value="1930">1930</option><option value="1929">1929</option><option value="1928">1928</option><option value="1927">1927</option><option value="1926">1926</option><option value="1925">1925</option><option value="1924">1924</option><option value="1923">1923</option><option value="1922">1922</option><option value="1921">1921</option><option value="1920">1920</option><option value="1919">1919</option><option value="1918">1918</option><option value="1917">1917</option><option value="1916">1916</option><option value="1915">1915</option><option value="1914">1914</option><option value="1913">1913</option><option value="1912">1912</option><option value="1911">1911</option><option value="1910">1910</option><option value="1909">1909</option><option value="1908">1908</option><option value="1907">1907</option><option value="1906">1906</option><option value="1905">1905</option><option value="1904">1904</option><option value="1903">1903</option><option value="1902">1902</option><option value="1901">1901</option><option value="1900">1900</option></select>
<br />
<div class="errormsg display_none" id="bd_err"><strong>Ongeldige geboortedatum.</strong></div><br>
<input type="hidden" name="referer" id="referer" value="#referer#" />
<input type="submit" id="reg_submit_button" value="Registreer" onmousedown="this.style.backgroundColor='#ddd';" onmouseup="this.style.backgroundColor='#eee';" onmouseover="this.style.backgroundColor='#eee';" onmouseout="this.style.backgroundColor='#fff';" />
</form>

Accessing variables of a dynamic form

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>

Django nested formsets

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.