Flask Radio buttons with choice for every user in table from database - flask

I tried to create a html table from flask jinja2 template. I'm able to get all the data from database and make a table with rows and columns and radio buttons. But I'm unable to get choice for every user or row that is added to the table.
Unable to figure out how to do that. Should I make use of javascript,jquery or ajax to give radio button choice for every user or is there a way to solve this in flask template.
My code is as below:
<form class="form-horizontal" action="/check_approve" method="post" id="admin_approve">
<div class="table-responsive">
<table class="table table-bordered table-striped table-highlight">
<thead>
<tr>
<th>UserName</th>
<th>Privileges</th>
<th>Approve</th>
<th>Reject</th>
</tr>
</thead>
<tbody>
{% for item in data %}
<tr>
<td>{{item[0]}}<input type="hidden" name="empid" value={{item[0]}}></td>
<td>
<select name="Privileges" form="admin_approve">
<option value="empty"> </option>
<option value="HR">HR</option>
<option value="Compensation Analyst">Compensation Analyst</option>
<option value="HR Manager">HR Manager</option>
</select>
</td>
<td>
<div class='radio'>
<label><input type='radio' id='choice1' name='approve' value="approve" checked></label>
</div>
</td>
<td>
<div class='radio'>
<label><input type='radio' id='choice2' name='reject' value="reject" checked></label>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="wrapper">
<button type="submit" form ="admin_approve" class="btn btn-primary">Submit</button>
</div>
</form>
Below is the attached screenshot. Appreciate your help.
table

Related

Django shopping cart update

I am trying to send slg (as quanty when update shopping cart):
{% for item in myCart.values %}
<tr>
<td class="product-col">
<img src="/media/{{item.image}}" alt="">
<div class="pc-title">
<h4>{{item.name}}</h4>
<p>${{item.price}}</p>
</div>
</td>
<td class="quy-col">
<div class="quantity">
<div class="pro-qty">
<input type="number" min="1" name="slg[{{item.id}}]" value="{{item.slg}}">
</div>
</div>
</td>
<td class="size-col"><h4>Size M</h4></td>
<td class="total-col"><h4>${% widthratio item.price 1 item.slg %}</h4></td>
<td class="total-col">Delete</td>
</tr>
{% endfor %}
This is my view, i am trying to get data form templates but it's not work
def updateCart(request):
slg = request.POST['slg']
return render(request, 'home/check.html', {'myCart': slg})
I don't see the section about the way you submit the data. You might want to build a form
like below
<form action="/your-name/" method="post">
<label for="your_name">Your name: </label>
<input type="submit" value="OK">
</form>
and post the data so when you check the request.POST that it has some data in it.
With your code, I don't see any data being posted.

how to pass the selected check box id of a input tag to views in django and bulk update the selected rows in table

** 1.how to pass the selected check box id of a input tag(which is in for loop, so the name and value of input tag are dynamic ) to views in django and bulk update the selected rows of table in django**
<div class="jumbotron">
<form method="post" action="">
{% csrf_token %}
{{form|crispy}}
<input class = "btn btn-primary" type="submit" value="Search">
</form>
<h3 > Model values </h3>
<ul>
<table class="table" id="tab1">
<thead>
{% if teams %}
<tr>
<th>Select</th>
<th>#</th>
<th><b>Logo</b> </th>
<th><b> Team </b></th>
</tr>
</thead>
<tbody>
{% for value in models %}
<tr>
<td><input name="lol" type = "checkbox" value = "{{value.id}}"/> </td>
<td> {{forloop.counter}}</td>
<td> <img class="w3-display-topmiddle w3-container" src="{{ value.logUri.url }}" alt="alt txt" height="910" width="910"></td>
<td> {{ value.name }} </td>
</tr>
{% endfor %}
</tbody>
</table>
**realized that since i am not keeping input tag in form tag the input tag's name values is not captured by request.POST(now changes are dome in html as below ),later in django views i removed the unnecessary keysvalues using dict.pop() and created a list containing on primary keys
in django orm (model.filter(id__in= lst_of_id_captured).update(field='somevalue') **
<form method="post" action="">
{% csrf_token %}
{{form|crispy}}
<h3 > Model values </h3>
<ul>
<table class="table" id="tab1">
<thead>
{% if teams %}
<tr>
<th>Select</th>
<th>#</th>
<th><b>Logo</b> </th>
<th><b> Team </b></th>
</tr>
</thead>
<tbody>
{% for value in models %}
<tr>
<td><input name="lol" type = "checkbox" value = "{{value.id}}"/> </td>
<td> {{forloop.counter}}</td>
<td> <img class="w3-display-topmiddle w3-container" src="{{ value.logUri.url }}" alt="alt txt" height="910" width="910"></td>
<td> {{ value.name }} </td>
</tr>
{% endfor %}
</tbody>
</table>
<input class = "btn btn-primary" type="submit" value="Search">
</form>

validate display none in django

template is
<button type="submit" id="add" class="button_style" onclick="addreporter();" value="Add New Authorised Reporter">Add New Authorised Reporter</button>
<div id="authorisedreporter" style="display:none">
<form method="post" action="{% url incident.views.about_me %}">{% csrf_token %}
<table width="100%">
<tr>
<td style="width:100px;">First name:</td>
<td>{{registerform.first_name}}{{registerform.first_name.errors}}</td>
</tr>
<tr>
<td>Last name:</td>
<td>{{registerform.last_name}}{{registerform.last_name.errors}}</td>
</tr>
<tr>
<td>Daytime phone:</td>
<td>{{createprofile.phone_daytime}}{{createprofile.phone_daytime.errors}}</td>
</tr>
<tr>
<td>Mobile phone:</td>
<td>{{createprofile.phone_mobile}}{{createprofile.phone_mobile.errors}}</td>
</tr>
<tr>
<td>Email:</td>
<td>{{registerform.email}}{{registerform.email.errors}}</td>
</tr>
<tr>
<td>User name</td>
<td>{{registerform.username}}{{registerform.username.errors}}</td>
</tr>
<tr>
<td>Password</td>
<td>{{registerform.password}}{{registerform.password.errors}}</td>
</tr>
<tr>
<td colspan=2 "">
<input type="checkbox" name="is_qualified_firstaiders" style="margin: 0;vertical-align:middle" />Qualified First Aiders</td>
</tr>
</table>
</form>
</div>
js:
function addreporter(){
$("#authorisedreporter").toggle();
if ($("#authorisedreporter").is(":visible")) {
$("#authorisedreporter").show();
$("#add").hide();
}
}
Using the jquery toggle,the gets open on click of this <button>Add new Authorized Reporter</button>,every thing is fine except that if their is any error in form,the form is going to be in hide mode,if i want to see the errors again i need to click the <button>Add new Authorized Reporter</button>,its a small logic to set if errors are in form,the form should not hide.What i thought is if i validate the display:none in <div> will work but no idea how to validate it in template.
Thanks
You can do:
<div id="authorisedreporter" {% if not registerform.errors %}style="display:none"{% endif %}>
Also, you might have to modify addreporter() accordingly.

Django: create generic link from radio select

i have a index.html that display all databases i own. And now i wanna give some options to handle the database (e.g. edit, delete, ...). So i wanna create a radio select with the database-ids and some links for redirecting the right url.
My aim: click on the requested action brings the user to the right url.
I hope, that was understandable :)
Can anyone help me, please.
// edited
<script>
function edit_db(id){
window.location.href="/edit/"+id
}
function delete_db(id){
window.location.href="/"+id+"/delete/"
}
</script>
<form method="post" action="">
<ul>
<table>
<tr>
<th> </th>
<th>Database</th>
<th>expireDate</th>
</tr>
{% for db in resources %}
<tr>
<td>
<input type="radio" name="id" id="{{db.id}}" value="{{db.id}}">
</td>
<td>{{db.name}}</td>
<td>{{db.expireDate}}</td>
</tr>
{% endfor %}
</table>
<input type="submit" class="submitButton" onclick="edit_db({{ db.id }})" value="{% trans "Edit database"%}" />
<input type="submit" class="submitButton" onclick="delete_db({{ db.id }})" value="{% trans "Delete database"%}" />
</form>
I'd suggest you use jQuery or so to accomplish this.
However, you need to adjust your id's first, because an id/class can't start with a number in html, so rather use id="database_{{ db.id }}"
I don't know what {% show_task_link "./{{db.id}}/edit" _("Edit database") %} does, but make sure it has an id so you can access it through jQuery which would be something like:
<script type="text/javascript>
$(function(){
$('.db').click(function(){
if($(this).is(':checked')){
$('#id_for_your_link').html() // put your link in html, add the id through $(this).val()
}
}
})
</script>
Note: i haven't tested the jQuery
<script>
function edit_db(id){
window.location.href="/edit/"+id
}
function delete_db(id){
window.location.href="/"+id+"/delete/"
}
</script>
<form method="post" action="">
<ul>
<table>
<tr>
<th> </th>
<th>Database</th>
<th>expireDate</th>
</tr>
{% for db in resources %}
<tr>
<td>
<input type="radio" name="id" id="{{db.id}}" value="{{db.id}}">
</td>
<td>{{db.name}}</td>
<td>{{db.expireDate}}</td>
<td>
<input type="submit" class="submitButton" onclick="edit_db({{ db.id }})" value="{% trans "Edit database"%}" />
<input type="submit" class="submitButton" onclick="delete_db({{ db.id }})" value="{% trans "Delete database"%}" />
</td>
</tr>
{% endfor %}
</table>
</form>

How to display option-names instead of option-values?

I poked around with get_FOO_display to no avail, here's what I've got (I trimmed some stuff to make it easier to read, like a custom def save:
class Profile(models.Model):
LEVEL_CHOICES = (
(1, 'Primary'),
(2, 'Secondary'),
(3, 'Tertiary'),
)
level = models.IntegerField( verbose_name = 'Level', blank=True, null=True, choices=LEVEL_CHOICES)
piplevel = models.ForeignKey(AccessLevel, related_name='profiles')
vanity = models.ForeignKey(AccessLevel, related_name='vanity')
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
# HTML CODE from Template below
<tbody>
{% for field in user_form %}
<tr>
<th>{{ field.label_tag }}</th>
<td class="updatable">
<div class="field">
{{ field }}
</div>
{% if field.errors %}{{ field.errors }}{% endif %}
</td>
</tr>
{% endfor %}
{% for field in profile_form %}
<tr>
<th>{{ field.label_tag }}</th>
<td class="updatable">
<div class="field">
{{ field }}
</div>
{% if field.errors %}{{ field.errors }}{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
The output I get:
Level 1
Piplevel 15
Vanity 20
The output I want:
Level Primary
Piplevel Diamond
Vanity Ambassador
I tried replacing the {{ field }} and for loop with {{field.get.level.display}} and {{ field.get_piplevel_display }} but it they just returned blanks. When these display, they're a drop-down menu and DO have all the named-options available - that is, when I click on the "15" I do get an option list i.e.:
Piplevel 15 click (current selected is 15)(option value="1">Regular(/option>(option value="2">Platinum(/option>etc. (not html code)
Am I missing some easy function in Django for this?
Edit: Here's the HTML output I GET currently.
<tr class="even">
<th>
<label for="id_piplevel">Piplevel</label>
</th>
<td class="updatable" title="Click to modify">
<div class="field" style="display: none;">
<select id="id_piplevel" name="piplevel">
<option value="">---------</option>
<option value="1">Regular</option>
<option value="2">Platinum</option>
...
<option selected="selected" value="7">Emerald</option>
</select>
</div>
<div>7</div>
</td>
</tr>
Here's the HTML output I WANT, ideally:
<tr class="even">
<th>
<label for="id_piplevel">Piplevel</label>
</th>
<td class="updatable" title="Click to modify">
<div class="field" style="display: none;">
<select id="id_piplevel" name="piplevel">
<option value="">---------</option>
<option value="1">Regular</option>
<option value="2">Platinum</option>
...
<option selected="selected" value="7">Emerald</option>
</select>
</div>
<div>Emerald</div>
</td>
</tr>
And, if I replace {{ field }} with {{field.get_piplevel_display}} or {{field.choice.get_piplevel_display}}, I get this as output:
<tr class="even">
<th>
<label for="id_piplevel">Piplevel</label>
</th>
<td class="updatable">
<div class="field"> </div>
</td>
</tr>
It seems like this is something everyone would want, and so I expected there to be some argument in Django that just... fixes this, but I can't find it. Sorry for making a simple question seem so complicated - this is supposed to be a simple question, isn't it?
For every field that has choices, Django automatically adds a get_FOO_display() method on the model (docs). All this method does is take the already stored value for the field, looks it up in the provided choices for the field and returns the "display" text for that value.
This method is not available on the field or the form. You must reference an existing object or the instance of the form (e.g. form.instance).
Again, I'm not sure what you're trying to accomplish, because the <select>s are already populated such that they display the text and not the underlying value. Even if you're editing an existing object, the <select> will still show the display text for the currently set value.
I don't really see what you're trying to do, but:
{{field.get.level.display}}
should be something like
{{field.choices.get_level_display}}
# or
{{field.get_level_display}}