Remember radio button selection in Django - django

Without using javascript, is there a better way to remember a user's radio button selection? I feel I have a pretty naive(?) way of doing it?
How can I re-write this to make it more DRY? usersegment parem gets returned on the same page upon submit.
{% if usersegment == "non-paying" %}
<input type="radio" name="usersegment" value="non-paying" checked="yes"/> Non-paying Users <br />
<input type="radio" name="usersegment" value="paying" /> Paying Users <br />
<input type="radio" name="usersegment" value="all" /> All Users<br />
{% endif %}
{% if usersegment == "paying" %}
<input type="radio" name="usersegment" value="non-paying" /> Non-paying Users <br />
<input type="radio" name="usersegment" value="paying" checked="yes"/> Paying Users <br />
<input type="radio" name="usersegment" value="all" /> All Users<br />
{% endif %}
{% if usersegment == "all" or not usersegment %}
<input type="radio" name="usersegment" value="non-paying" checked="yes"/> Non-paying Users <br />
<input type="radio" name="usersegment" value="paying" /> Paying Users <br />
<input type="radio" name="usersegment" value="all" checked="yes"/> All Users<br />
{% endif %}

How about this:
<input type="radio" name="usersegment" value="non-paying" {% if usersegment == "non-paying" %}checked="checked"{% endif %}/> Non-paying Users <br />
<input type="radio" name="usersegment" value="paying" {% if usersegment == "paying" %}checked="checked"{% endif %}/> Paying Users <br />
<input type="radio" name="usersegment" value="all" {% if usersegment == "all" or not usersegment %}checked="checked"{% endif %}/> All Users<br />
Also, consider using django forms. Trivial problems like those are solved in a very elegant way there, you don't even need to think about it.

Related

How can I access the outer loop context value in inner loop section in Django Template

In my case, I want to access the context value which is present in the outer for loop and use(print) that value in the inner for loop section. There are two context values one for the outer loop and the other one is for the inner loop.
here is my template logic. when the condition becomes false it did not print the values that belong to the outer-loop context variable(else part of if statement not working).
<div class="panel-group" id="accordion">
{% for x in queryset %}<!--outerloop-->
<div id="collapse{{ forloop.counter }}" class="panel-collapse collapse">
<div class="panel-body">
<div>
<form action="tobeupdated" method="POST" enctype="multipart/form-data" >
{% csrf_token %}
<label>Q- <input type="text" id="ques" name="ques" value="{{x.question.question}}" readonly></label><br><br>
<img src="{{x.question.image}}" alt="" srcset="" width="700"><br><br>
{% for q2 in queryset2 %}<!--innerloop-->
{% if q2.option_id == x.option_id %}
<label>A- <input type="text" id="op1" name="op1" value="{{q2.option.option1}}" readonly><br><br>
<img src="{{q2.option1_img}}" alt="" srcset="" width="400"><br><br>
<label>B- <input type="text" id="op2" name="op2" value="{{q2.option.option2}}" readonly><br><br>
<img src="{{q2.option2_img}}" alt="" srcset="" width="400"><br><br>
<label>C- <input type="text" id="op3" name="op3" value="{{q2.option.option3}}" readonly><br><br>
<img src="{{q2.option3_img}}" alt="" srcset="" width="400"><br><br>
<label>D- <input type="text" id="op4" name="op4" value="{{q2.option.option4}}" readonly><br><br>
<img src="{{q2.option4_img}}" alt="" srcset="" width="400"><br><br>
{% else %}
<label>A- <input type="text" id="op1" name="op1" value="{{x.option.option1}}" readonly><br><br>
<label>B- <input type="text" id="op2" name="op2" value="{{x.option.option2}}" readonly><br><br>
<label>C- <input type="text" id="op3" name="op3" value="{{x.option.option3}}" readonly><br><br>
<label>D- <input type="text" id="op4" name="op4" value="{{x.option.option4}}" readonly><br><br>
{% endif %}
{% endfor %}
<label>Correct Ans- <input type="text" id="ans" name="ans" value="{{x.correct_ans}}" readonly><br><br>
<input type="hidden" id="dlevel" name="dlevel" value="{{x.question.difficulty}}">
<input type="checkbox" name="" checked>
<label>{{x.question.difficulty}}</label>
<!-- <input type="submit" id="update" value="Update" /> -->
<button type="submit" class="btn btn-primary" name="qid" value="{{x.question.id}}" >Edit</button>
<button type="submit" class="btn btn-primary" name="delete_qid" value="{{x.question.id}}" >Delete</button>
</form>
</div>
<!--Toogle Button-->
</div>
</div>
{{x.question.id}}
<!-- Button trigger modal -->
</div>
</div>
{% endfor %}
</div>
Any suggestions regarding this problem will be highly appreciated.

Using GeoLocation variables in CFSET

I'm trying to capture geolocation data and insert it into a database.
The code below is my attempt to capture the browser's longitude and latitude and save the values in two form fields. The problem is the output code generates multiple instances of the two form fields (i.e. "longitude" and "longitude"). Here's a screen shot of a single set of those fields being populated. How do I populate the rest?
Form Code:
<div class="table-responsive">
<table class="table table-1" style="color:##000000">
<tr>
<td>Tree No</td>
<td>Current Status</td>
<td>Graft Successful</td>
<td>NOT Grafted</td>
<td>Graft unsuccessful</td>
<td>Tree Died</td>
</tr>
<cfoutput query="rsTreeList" >
<tr>
<td>#TreeID#</td>
<td>#Status#</td>
<td>
<form name="form1" method="post" action="Recon_Update_Logic1.cfm?ID=#TreeID#">
<img src="images/Icons/Success.jpg" width="25" height="25" alt=""/>
<input id="latitude" name="latitude" type="text" />
<input id="longitude" name="longitude" type="hidden" />
<input name="submit" type="submit" id="Submit" value="Graft Successful">
</form>
</td>
<td>
<form name="form1" method="post" action="Recon_Update_Logic2.cfm?ID=#TreeID#">
<img src="images/Icons/NotGrafted.jpg" width="25" height="25" alt=""/>
<input id="latitude" name="latitude" type="text" />
<input id="longitude" name="longitude" type="hidden" />
<input name="submit" type="submit" id="Submit" value="NOT Grafted">
</form>
</td>
<td>
<form name="form1" method="post" action="Recon_Update_Logic3.cfm?ID=#TreeID#">
<img src="images/Icons/GraftUnsuccessful.jpg" width="25" height="25" alt=""/>
<input id="latitude" name="latitude" type="hidden" />
<input id="longitude" name="longitude" type="hidden" />
<input name="submit" type="submit" id="Submit" value="Graft Unsuccessful">
</form>
</td>
<td>
<form name="form1" method="post" action="Recon_Update_Logic4.cfm?ID=#TreeID#">
<img src="images/Icons/TreeDead.jpg" width="25" height="25" alt=""/>
<input id="latitude" name="latitude" type="hidden" />
<input id="longitude" name="longitude" type="hidden" />
<input name="submit" type="submit" id="Submit" value="Tree Dead">
</form>
</td>
</tr>
</cfoutput>
</table>
</div>
The following code attempts to populate all of the "latitude" and "longitude" elements:
<cfloop index="i" from="1" to="#rsTreeList.recordCount#">
<script>
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position)
{
document.getElementById("latitude").value = position.coords.latitude;
document.getElementByID("longitude").value = position.coords.longitude;
}
getLocation();
</script>
</cfloop>

Multistep Form in django

I was working with normal crispy forms in django. I changed the form to multi step form, now the submit doesnt work. So when i click on submit data doesnt get stored in the db.
This is how the updated template looks like: index.html
index.html
<ul id="progressbar">
<li class="active">Account Setup</li>
<li>Social Profiles</li>
<li>Personal Details</li>
</ul>
<!-- fieldsets -->
<fieldset>
<h2 class="fs-title">Help me know you better</h2>
<h3 class="fs-subtitle">This is step 1</h3>
{{ form.associate_image|as_crispy_field}}
<input type="button" name="next" class="next action-button" value="Next" />
</fieldset>
<fieldset>
<h2 class="fs-title">Social Profiles</h2>
<h3 class="fs-subtitle">Your presence on the social network</h3>
{{ form.title|as_crispy_field}}
{{ form.client|as_crispy_field }}
{{ form.lob|as_crispy_field }}
<input type="button" name="previous" class="previous action-button" value="Previous" />
<input type="button" name="next" class="next action-button" value="Next" />
</fieldset>
<fieldset>
<h2 class="fs-title">Personal Details</h2>
<h3 class="fs-subtitle">We will never sell it</h3>
{{ form.gender|as_crispy_field }}
{{ form.phone_number|as_crispy_field }}
<textarea name="address" placeholder="Address"></textarea>
<input type="button" name="previous" class="previous action-button" value="Previous" />
<input type="submit" name="submit" id="submit-award" class="submit action-button" value="Submit" />
</fieldset>
This is the view function :
views.py
#login_required
def save_information(request):
associate_id = id_to_numeric(request.user.username)
ldap = Ldap()
current_user = UserDetails.objects.filter(associate_nbr=associate_id)
if current_user.exists():
current_user.delete()
associate_details = ldap.search(associate_id=associate_id)[0]
details = UserDetails(
associate_name=associate_details['name'],
associate_nbr=associate_id,
associate_email=associate_details['email'],
associate_department_id=id_to_numeric(associate_details['department']),
associate_mgr=associate_details['xxx'],
associate_exec=associate_details['xxx'],
associate_org=associate_details['org'],
associate_image=request.FILES['associate_image'],
gender=request.POST['gender'],
title=request.POST['title'],
client=request.POST['client'],
lob=request.POST['lob'],
phone_number=request.POST['phone_number'] ,
)
details.save()
return redirect('/?submit=true')

Django form in a table can't get right parameters

<tbody>
{% for sec in sec_list %}
<tr>
<td>{{sec.c_id_id}}.{{sec.sec_id}}</td>
<td>{{sec.title}}</td>
<td>{{sec.name}}</td>
<td>{{sec.time}}</td>
<td>{{sec.r_no_id}}</td>
<td>{{sec.cur}}/{{sec.capcity}}</td>
<td><form method="post" role="form" action="">
{% csrf_token %}
<input class="hidden" type="submit" value="{{sec.c_id_id}}.{{sec.sec_id}}" name="course" id="course">
<input class="hidden" type="submit" value="{{sec.cur}}" name="num" id="num">
<input class="hidden" type="submit" value="{{sec.capcity}}" name="limit" id="limit">
<p class="form-action">
<input type="submit" value="选课" class="btn btn-link">
</p>
</form></td>
</tr>
{% endfor %}
</tbody>
I want to use a hidden form to transfer some parameters in a table.My code is above.But when I use
request.POST.get("limit",'')
,I got a null one.How to fix it
edit your html code by replacing the type="submit" to type="hidden",
<td><form method="post" role="form" action="">
{% csrf_token %}
<input class="hidden" type="hidden" value="{{sec.c_id_id}}.{{sec.sec_id}}" name="course" id="course">
<input class="hidden" type="hidden" value="{{sec.cur}}" name="num" id="num">
<input class="hidden" type="hidden" value="{{sec.capcity}}" name="limit" id="limit">
<p class="form-action">
<input type="submit" value="选课" class="btn btn-link">
</p>
</form></td>
Shouldn't those inputs be type=hidden instead of class=hidden?
<input type="hidden" value="{{sec.capcity}}" name="limit" id="limit">
The type=submit in all of them makes no sense to me as it is supposed to be used only in submit buttons.

django html form that is showing error this field is required

the post method is not getting the username record. the get method is working fine but html form is showing the following error:
This field is required
<h2>Users</h2>
<ul>
<form method="post" action=""><input type='hidden' name='csrfmiddlewaretoken' value='8gQo0iGRTDE7kayhFJqj2fOt7UkejlkG' />
<li>
<input type="text" value= mayur><br>
<input type="submit" value="follow" />
</li>
<li>
<input type="text" value= mayurnitrr><br>
<input type="submit" value="follow" />
</li>
<li>
<input type="text" value= lokesh><br>
<input type="submit" value="follow" />
</li>
</form>
</ul>
<p align="center">Back</p>
You haven't shown how you handle these but none of your inputs have a name parameter which you need to provide
<input type="text" name="somethingdescriptive" value="lokesh"><br>
request.POST should now contain the key "somethingdescriptive"