Django: create generic link from radio select - django

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>

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 update django databsae from html?

I want to only update the brand. So I want to see if I can get the post from the input.
print(request.POST.get('brand')) didn't print anything, but I can get print(request.POST.get('url')). Does anyone know why?
Here is my base.html code.
<form action="{% url 'script' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group w-75">
<input type="text" name="url" id="url" class="form-control">
</br>
<button type="submit" class="btn btn-success btn-lg btn-block">submit</button>
</div>
</br>
{% if alldata %}
<table class="table table-striped table-sm" id="">
<thead>
<tr>
<th>Price</th>
<th>Asin</th>
<th>Rank</th>
<th>Brand</th>
<th>CPU</th>
<th>Update</th>
</tr>
</thead>
<tbody>
{% for data in alldata %}
<tr>
<td>{{ data.price }}</td>
<td> {{ data.asin }} </td>
<td>{{ data.rank }}</td>
<td>
<div>
{{ data.brand }} <input type="text" name="brand" id="brand" value="{{data.brand}}">
<button type="submit">Update</button>
</div>
</td>
<td>cpu</td>
<td><button type="submit">Update</button></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</form>
To check All data in request.POST:
print(request.POST)
to print only one 'field'
print(request.POST['field'])

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>

Call two different urls from a form or input submit button Django

I have a form in which I have a table.In table I have a record(title).Now along with title I have two buttons delete and edit.Now I want is that, if I click edit it calls separate url along with primary key going through it or if I click delete It calls separate url with primary key going through.
here's my code:
<form method="POST" action="{% url 'essay:delete_view' teacher.id %}">
<div class="page-header">
<div class="span4">
<h1>Manage Essays</h1>
</div>
</div>
<table>
<tr>
<th>Title</th>
<th>Edit</th>
<th>Delete</th>
</tr>
{% csrf_token %}
{% for uploadassignment in teacher.uploadassignment_set.all %}
<tr>
<th for="uploadassignment{{ forloop.counter }}">{{uploadassignment.id}}</th>
<th><input type="submit" id="Editthis" value="{{uploadassignmentEdit.id}}" class="btn btn-primary"/></th>
<th><input type="submit" id="uploadassignment{{ forloop.counter }}" value="{{uploadassignment.id}}" name="uploadassignment" class="btn btn-primary"/></th>
</tr>
{% endfor %}
</table>
</form>
I want this: one url is called by below (Edit button) with teacher.id
<th><input type="submit" id="Editthis" value="{{uploadassignmentEdit.id}}" class="btn btn-primary"/></th>
And another url is called when hit delete button with teacher id.
<th><input type="submit" id="uploadassignment{{ forloop.counter }}" value="{{uploadassignment.id}}" name="uploadassignment" class="btn btn-primary"/></th>
So far due to action ="{% url 'essay:delete_view' teacher.id %}" in form I can switch to only one url.Kindly help in my code so I can switch to different urls with teacher id when clicked on delete or edit button.
urls.py:
url(r'^$', views.Indexview.as_view(), name='index'),
url(r'^login/$',views.loginform.as_view(),name='login'),
url(r'^addfile/$',views.Addfile.as_view(),name='file'), #essay/addfile
url(r'^addfile/$',views.EditEssay.as_view(),name='eEssay'),
url(r'^text/$',views.writetext.as_view(),name='text'),
url(r'^about/$',views.aboutus.as_view(),name='about'),
url(r'^stdprofile/$',views.studentprofile.as_view(),name='stdprofile'),
url(r'^logout/$', LogoutView.as_view(), name='user_logout'),
url(r'^(?P<teacher_id>[0-9]+)/delEssay/$', views.delete_view, name='delete_view'),

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

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