I have a form representing a quiz with some questions and a result that appears in the end in Django 1.10. I need t implement two other buttons next to the "Check" button, one of them is "Mark" and the other is "Back".
When I click on "back", I m handling well its behavior in the view but a pop up appears saying "Please Select One Of these Options".
I need to disable it since user should not select one of the radio buttons if going back to the previous question.
<form action="" method="POST">
{% csrf_token %}
<input type=hidden name="question_id" value="{{ question.id }}">
<ul class="list-group">
{% for answer in form.answers %}
<li class="list-group-item" >{{ answer }}</li>
{% endfor %}
</ul>
<div id="buttons">
<input type="submit" name="question_btn" value={% trans "Check" %} class="btn btn-lg center-block btn-outline btn-success" required>
<input type="submit" name="question_btn" value={% trans "Back" %}
class="btn btn-lg center-block btn-outline btn-success" required>
Any idea?
The back button shouldn't be an input and a type="submit". Just make it a button or a a element and that would work nicely.
Related
I am having a problem with Django forms. I am currently using the request.POST to validate the POST within views.
I want to send inputed-data(radio type) from my template with the same name.
In my template.py:
<form method="post" action="{% url 'app:AnsSubmission' %}">
{% csrf_token %}
{% for i in Exam %}
<div class="form-group col-xl-12">
<label>{{ i.question }}</label>
<div class="radio">
<label><input type="radio" class="mx-2" name="givenAns" array_column="{{ i.pk }} value="True" required>True</label>
</div>
<div class="radio">
<label><input type="radio" class="mx-2" name="givenAns" array_column="{{ i.pk }} value="False" required>False</label>
</div>
</div>
{% endfor %}
<button type="submit" class="btn btn-primary">Submit</button>
</form>
And willing to fetch these data in views.py like:
givenAns_list = request.POST.getlist('givenAns')
for i in givenAns_list:
this_givenAns = givenAns_list[i]
But the problem here is this radio type input field doesn't take value for the same name(not as a list I wish). If I select answer of 2nd question, the 1st one's selection is unselected.
Please suggest how can I fix this?
The problem is that I have two buttons in a form. Additionally, both buttons are running though the same function. How do i force the "Save" button to run though a different function when clicked?
I can post the python code later if necessary.
<form action="/predict" class="form-upload" method="post" enctype="multipart/form-data">
<h1 class="h3 mb-3 font-weight-normal">Please Upload Image</h1>
<input autofocus class="form-control" id="image" name="image" required type="file"><br>
<button type = "submit" name = "predict" formaction="/predict" class="btn btn-lg btn-primary btn-block">Predict</button>
<button type = "submit" name = "save" formaction = "/add" class="btn btn-lg btn-primary btn-block" >Save</button>
{% if image_loc %}
<img id="img" class="mb-4" src="static/{{ image_loc }}" alt="" width="256" height="256">
<h3 id="nm" class="h3 mb-3 font-weight-normal">Prediction: {{ p }}</h3><br>
{% endif %}
</form>
You can use formaction attribute on the button. It overrides the action attribute of the form.
MDN
How do i allow to display a particular form content after the user clicks on checkbox Yes or No
for e.g.if the user clicks on yes checkbox display the form to be filled if user clicks No display another form part to be filled
I want to add it here like in my applyonline.html
{% extends "pmmvyapp/base.html" %}
{% load crispy_forms_tags %}
{% load static %}
{% block content%}
<div class="col-md-8">
<form method="post" action="/add_aganwadi/">
{% csrf_token %}
<div class="form-group">
<div class=" mb-4">
<h6><u>Please Fill up the details below (Note that all the fields are required)</u></h6>
</div>
<legend class="border-bottom mb-4" ,align="center">Beneficiary Details</legend>
<label for="formGropuNameInput">Does Beneficiary have a Driving License Card?*</label>
<div class="form-check form-check-inline">
{% if..... %}
<input class="form-check-input" type="checkbox" id="inlineCheckbox1" value="option1">
<label class="form-check-label" for="inlineCheckbox1">Yes</label>
{% else %}
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="inlineCheckbox2" value="option2">
<label class="form-check-label" for="inlineCheckbox2">No</label>
</div>
</div>
{% endif %}
<button type="submit" class="btn btn-primary" style="margin-bottom:10px ">Submit</button>
</form>
</div>
{% endblock %}
I want to know if I can use the if statement to achieve this and if so how?
if you don't have any problem in including angularjs in form page and radio button instead of check box this below sample code image i used previously will help you
I have a read-only textbox which preloaded value from database which upon a button click sends it's value to a method present in backend to perform DELETE query of sql. The problem is occuring when I am click on the button the method is invoked but the request.method condition is not invoked. It is directly going to the end return statement of the method.
#app.route('/home/delete_reminder/<string:id_val>',methods=['GET','POST'])
#is_logged_in
def delete_reminder(id_val):
if request.method=='POST':
desc = request.form['description']
x = desc.split(',')
cur = mysql.connection.cursor()
cur.execute('DELETE FROM set_reminder WHERE DATE=%s,SUBJECT=%s,DESCRIPTION=%s',[x[0],x[1],x[2]])
cur.execute('DELETE FROM recur WHERE RECUR_NEXT=%s',[id_val])
flash('Reminder Deleted','danger')
mysql.connection.commit()
cur.close()
return redirect(url_for('search_reminder_to_delete'))
This is my backend code.
<form method="POST">
{% for data in value %}
<div class="form-group">
<label>Description</label>
<input type="text" name="description" class="form-control" readonly="true" value="{{data.DATE}},{{data.SUBJECT}},{{data.DESCRIPTION}}">
</div>
Delete Reminder
{% endfor %}
</form>
This is the html part.
Your button isn't a button, it's a link. You aren't submitting your form.
If you want to fo that then you need to make you form tag:
<form method="POST" action="/home/delete_reminder/{{data.RECUR_NEXT}}">
and switch your button to be a real button that submits the form:
<div class="button">
<button type="submit" class="btn btn-warning">Send your message</button>
</div>
EDIT: Seeing that you want to have multiple possible routes for your form based on the loop.
You could try and use the formaction attribute, although it isn't going to be supported by every browser version.
<form method="POST">
{% for data in value %}
<div class="form-group">
<label>Description</label>
<input type="text" name="description" class="form-control" readonly="true" value="{{data.DATE}},{{data.SUBJECT}},{{data.DESCRIPTION}}">
</div>
<div class="button">
<button formaction="/home/delete_reminder/{{data.RECUR_NEXT}}" class="btn btn-warning" type="submit" class="btn btn-warning">Delete Reminder</button>
</div>
{% endfor %}
However this will still result in your description field that is passed to the request having every single description from the whole form in a list or possibly just the last one as a single value (I can't quite remember the behaviour of multiple inputs with the same name), which I don't think is what you're expecting to happen.
It may just be easiest to create a separate form for each link in a loop to be honest:
{% for data in value %}
<form method="POST" action="/home/delete_reminder/{{data.RECUR_NEXT}}">
<div class="form-group">
<label>Description</label>
<input type="text" name="description" class="form-control" readonly="true" value="{{data.DATE}},{{data.SUBJECT}},{{data.DESCRIPTION}}">
</div>
<div class="button">
<button class="btn btn-warning" type="submit" class="btn btn-warning">Delete Reminder</button>
</div>
</form>
{% endfor %}
I'm having a problem where Django, or a form, is splitting object list items on spaces.
If the object_list is something like: ["picture"='image1.jpg "dir_as_title"='UnknownArtist', "picture"='image2.jpg' "dir_as_title"='Unknown Artist'] and I run it through the following:
<div>
{% for obj in object_list %}
<div class="col-md-6" style="background:url({{obj.picture}});
background-size: 100% 100%; min-height: 260px; max-width: 480px;" >
<form id="form" method="POST">
{% csrf_token %}
<input type="submit" value={{ obj.dir_as_title }}
name={{ obj.dir_as_title }} class="btn btn-primary btn-lg btn-block"/>
</form>
</div>
{% endfor %}
</div>
The first item will be fine, displaying 'UnknownArtist' in the button, and passing 'UnknownArtist' as its input. The second item will display the button as 'Unknown', and appears to pass no value, resubmitting its current state.
The answer ended up being embarrassingly simple. In other areas of the template, the double-brackets around a variable suffice, such as <h2>{{ title }}</h2> but in the form, they must be quoted. Using...
<input type="submit" value="{{ obj.dir_as_title }}"
name="{{ obj.dir_as_title }}" class="btn btn-primary btn-lg btn-block"/>
...works as expected, that is, it's no longer splitting the value. I hope this saves someone else from all the searches I did.