multi modals bootstrap in for loop django - django

I have a for loop on my html code to display a list of data, when I made a Bootstrap Modal in this loop to display it for each element of the list. It seems that the modal work only for the first element of the list.
{% for i in lb %}
<button class="btn" id="myBtn" title="Ajouter Serveur" style="padding-bottom:1px" data-target="#myModal"><a class="icon-plus-sign" title="Ajouter Serveur"></a></button>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Ajouter Serveur</h4>
</div>
<div class="modal-body">
<form method="post" class="loginForm">
<input type="hidden" name="lb" value="{{ i.Nom }}">
<h6>{% csrf_token %}
{{ form.as_table }}</h6>
<input type="submit" name="submit" class="btn btn-primary " value="Submit" />
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div></div>
{% endfor %}

The id value of the button and <div class="modal"> have to be unique in the document.
Try adding a unique value to the id, ie:
<div class="span12">
{% for i in lb %}
<div id="Person-1" class="box">
<div class="box-header">
<div class="span10">
<i class="icon-hdd icon-large"></i>
<h5>{{i.Nom}}</h5></div>
<button class="btn" id="myBtn{{i.id}}" title="Ajouter Serveur" style="padding-bottom:1px" data-target="#myModal{{ i.id }}"><a class="icon-plus-sign" title="Ajouter Serveur"></a></button>
<div class="modal fade" id="myModal{{ i.id }}" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Ajouter Serveur</h4>
</div>
<div class="modal-body">
<form method="post" class="loginForm">
<input type="hidden" name="lb" value="{{ i.Nom }}">
<h6>{% csrf_token %}
{{ form.as_table }}</h6>
<input type="submit" name="submit" class="btn btn-primary " value="Submit" />
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div></div>
</div>
<div class="box-content box-table">
<table class="table table-hover tablesorter">
<thead>
<tr>
<th>Nom</th>
<th>Adresse</th>
<th>Etat</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{% for j in s %}
{% if i.Nom == j.LB %}
<tr>
<td>{{ j.Nom }}</td>
<td>{{ j.Adresse }}</td>
{% if j.Etat == m %}
<td>Désactiver</td>
{% else %}
<td>Activer</td>
{% endif %}
<td><a>Edit</a></td>
<td>Supprimer</td>
{% if j.Etat == m %}
<td class="icon-ok-circle icon-large" style="color : green" title="Activé"></td>
{% else %}
<td class="icon-remove-circle icon-large" style="color : red" title="Désactivé"></td>
{% endif %}
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
</div>
<script>
var a = "{{ i.id }}"
$(document).ready(function(){
$("#myBtn"+a).click(function(){
$("#myModal"+a).modal({show:true});
});
});
</script>
{% endfor %}
</div>

In HTML id have unique identification. So we use unique id in every modal
{% for l in lamp %}
<h6>change in button</h6>
<button data-target="#mymodal{{l.id}}"</button>
<h6>change in modal id </h6>
<div class="modal" id="mymodal{{l.id}}"</div>
{% endfor %}

Related

How to use a Bootstrap Modal to delete in Django?

Must I add to JavaScript section or not?
HTML
</head>
<body>
<div class="container-xl">
<div class="table-responsive">
<div class="table-wrapper">
<div class="table-title">
<div class="row">
<div class="col-sm-6">
<h2><b>Shipping</b></h2>
</div>
<div class="col-sm-6">
</i> <span>Add New Shipping</span>
<div class="col-sm-6">
<div class="search-box">
<div class="input-group">
<input type="text" id="search" class="form-control" placeholder="Search by Name">
<span class="input-group-addon"><i class="material-icons"></i></span>
</div>
</div>
</div>
</div>
</div>
</div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Destination</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for shipping in shipping_info %}
<tr>
<td>{{ shipping.shipping_id }}</td>
<td>{{ shipping.driver_id.driver_fname }} {{ shipping.driver_id.driver_lname }}</td>
<td>{{ shipping.destination }}</td>
<td>{{ shipping.ship_date }}</td>
<td>
</i>
<i class="material-icons" data-toggle="tooltip" title="Delete"></i>
<a href="{% url 'view_shipping' shipping.shipping_id %}" class="" ><span class="material-icons">
visibility
</span></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
HTML:
<!-- Delete Modal HTML -->
<form method="post" action="">
{% csrf_token %}
<div id="deleteEmployeeModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<form>
<div class="modal-header">
<h4 class="modal-title">Delete User</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<p>Are you sure you want to delete these Records?</p>
<p class="text-warning"><small>This action cannot be undone.</small></p>
</div>
<div class="modal-footer">
<input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
<input type="submit" class="btn btn-danger" value="Delete">
</div>
</form>
</div>
</div>
</div>
</form>
{% endblock %}
</body>
</html>
I can delete but it doesn't have modal so I added this but when I add it can't delete.
view
def delete_shipping(request,pk):
del_dri = shipping.objects.get(shipping_id=pk)
del_dri.delete()
return redirect('table_shipping')
The problem is a funny error in the modal form
Url
path("delete_shipping/<str:pk>",views.delete_shipping,name='delete_shipping'),
You're tried change input:
<div class="modal-footer">
<input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
<input type="submit" class="btn btn-danger" value="Delete">
</div>
to:
<div class="modal-footer">
<input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
<a hrfe= "{% url "delete_shipping" %}" class="btn btn-danger">Delete </a>
</div>
?

How can I remove a particular item using a popup?

I have many items and I want to delete one of them but when I delete one item it turns out that it deletes the last item which exists based on ordering so, if I have 10 records of id which start from 1 to 10 record so, it will remove the item number 10 til if I remove item number 5 will remove the number 10. this case occurs because of popup but if I remove popup and delete the items directly it will remove with no mistake so, How can I remove a particular item using popup?
profile.html
{% if request.user == profile.user %}
<div class="col-lg-7 offset-lg-1 col-12">
{% if profile.user.user_history.all.count != 0 %}
<form method="post">
{% csrf_token %}
<div class="clear_all fl-left">
<input class="global_checkbox" type="checkbox" name="remove_all_history" id="remove_all_history">
<label for="remove_all_history" class="ml">Remove All</label>
</div>
<div class="fl-right">
<input type="submit" value="Remove" class="clear_button btn btn-danger invisible"/>
</div>
</form>
{% else %}
<p class="text-center">you have no history yet!</p>
{% endif %}
<div class="clearfix"></div>
<div class="mt-6"></div>
{% for history in history_pages %}
{% if history.deleted_history == False %}
<div class="history">
<div class="row">
<div class="col-4">
<form method="post">
{% csrf_token %}
<input class="global_checkbox" type="checkbox" name="remove_one_history" id="remove_all_history">
<span class="ml-2">{{ history.history_time|time }}</span>
<div class="ml ml-4">{{ history.history_time|date:'d M Y' }}</div>
</form>
</div>
<div class="history-content col-7">
<p><strong>text:</strong> {{ history.history }}</p>
<p><strong>action:</strong> {{ history.action_option }}</p>
<p><strong>position:</strong>
{% if history.verb_option == "" %}
POS
{% else %}
{{ history.verb_option }}
{% endif %}
</p>
</div>
<form method="post" action="{% url 'accounts:remove_history' history.id %}">
{% csrf_token %}
<div class="history-list col-1">
<span class="fa fa-ellipsis-v" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></span>
<div class="dropdown-menu">
<a type="button" class="dropdown-item" data-toggle="modal" data-target="#exampleModal">Remove this item</a>
</div>
</div>
{% include 'accounts/popup.html' %}
</form>
</div>
</div>
{% endif %}
{% endfor %}
</div>
{% endif %}
popup.html
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Warning!!</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Do you want to remove this history item?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-outline-danger">Remove</button>
</div>
</div>
</div>
</div>
views.py
#login_required
def userProfile(request, slug=None):
profile = None
try:
profile = Profile.objects.get(user__slug=slug)
paginator = Paginator(profile.user.user_history.all(), 100)
page_number = request.GET.get('page_number')
history_pages = paginator.get_page(page_number)
except:
return redirect('accounts:index_404')
return render(request, 'accounts/profile.html', {'profile': profile, 'history_pages': history_pages})
def remove_history(request, id=None):
if id and id is not None:
# History.objects.get(id=id)
print(id)
return redirect("accounts:profile", request.user.username)
Note: I tested the delete using print(id)
In your current code you have included popup.html mutliple times so when you click on a tag its not confirm which modal will get open has all are triggering exampleModal i.e :data-target="#exampleModal" .
So , to overcome this one way would be including only one modal and adding form tags around submit button . Then , whenever user click on a tag you can get action value from form where a tag has been clicked and then add this action value inside modal form tag .
Demo Code :
//on click of `a` tag
$(".dropdown-item").on("click", function() {
//get closest form from `a` tag then get action from it
var action_ = $(this).closest("form").attr("action");
$("#exampleModal form").attr("action", action_) //add that action inside modal form tag
console.log(action_)
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<div class="history">
<div class="">
<div class="">
<form method="post">
{% csrf_token %}
<input class="global_checkbox" type="checkbox" name="remove_one_history" id="remove_all_history">
<span class="">12:30</span>
<div class="">12-04-21</div>
</form>
</div>
<div class="history-content">
<p><strong>text:</strong> Somethigs</p>
<p><strong>action:</strong>Ok</p>
<p><strong>position:</strong> POS
</p>
</div>
<form method="post" action="{% url 'accounts:remove_history' 1 %}">
<div class="history-list">
<span class="fa fa-ellipsis-v" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></span>
<div class="dropdown-menu">
<a type="button" class="dropdown-item" data-toggle="modal" data-target="#exampleModal">Remove this item</a>
</div>
</div>
<!--remove this line {% include 'accounts/popup.html' %}-->
</form>
</div>
</div>
<div class="history">
<div class="">
<div class="">
<form method="post">
{% csrf_token %}
<input class="global_checkbox" type="checkbox" name="remove_one_history" id="remove_all_history">
<span class="">12:32</span>
<div class="">22-04-21</div>
</form>
</div>
<div class="history-content">
<p><strong>text:</strong> Somethigs2</p>
<p><strong>action:</strong>Ok2</p>
<p><strong>position:</strong> POS
</p>
</div>
<form method="post" action="{% url 'accounts:remove_history' 2 %}">
<div class="history-list">
<span class="fa fa-ellipsis-v" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></span>
<div class="dropdown-menu">
<a type="button" class="dropdown-item" data-toggle="modal" data-target="#exampleModal">Remove this item</a>
</div>
</div>
<!--remove this line {% include 'accounts/popup.html' %}-->
</form>
</div>
</div>
<!--just use only one modal no need to include it every time on your page-->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Warning!!</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<!--added form tag-->
<form method="post" action="">
<!--added csrf token-->
{% csrf_token %}
<div class="modal-body">
Do you want to remove this history item?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-outline-danger">Remove</button>
</div>
</form>
</div>
</div>
</div>

How to list the id with getlist method in Django checkbox

html file
Here I list the client and Process in the input tag
And select the and submit
{% extends 'client_pannel.html' %}
{% block content %}
<script>
function postConfirm() {
if (confirm('Are you sure you want to delete this client')) {
yourformelement.submit();
} else {
return false;
}
}
</script>
<form method="POST">
{% csrf_token %}
<div class="breadcome-area">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="breadcome-list single-page-breadcome">
<div class="row">
<div class="col-lg-10 col-md-6 col-sm-6 col-xs-12">
<div class="breadcome-heading">
<form role="search" class="sr-input-func">
<label>{{ emp.first_name }} {{ emp.last_name }}</label>
</form>
</div>
</div>
<div class="col-lg-2 col-md-6 col-sm-6 col-xs-12">
<a href="/employee_list/"> <button type="submit"
class="btn btn-info add-new">Submit</button></a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="data-table-area mg-b-15">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="sparkline13-list">
<div class="sparkline13-graph">
<div class="datatable-dashv1-list custom-datatable-overright">
<table id="table" data-toggle="table" data-pagination="true" data-search="true"
data-show-columns="true" data-show-pagination-switch="true" data-show-refresh="true"
data-key-events="true" data-show-toggle="true" data-resizable="true"
data-cookie="true" data-cookie-id-table="saveId" data-show-export="true"
data-click-to-select="true" data-toolbar="#toolbar">
<thead>
<tr>
<th>Process</th>
</tr>
</thead>
<tbody>
{% for client in form %}
<tr>
<td>
<input type="checkbox" name="cname[]" value="{{ client.id }}">
{{ client.Name }}
</td>
<td>
{% for process in client.clients %}
<input type="checkbox" name="process[]" value="{{ process.id }}">
{{ process.process }}<br />
{% endfor %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
{% endblock %}
views file
The below code is get the selected in checkbox. The getlist method return nothing
if request.method == "POST":
cname = request.POST.getlist('cname[]')
print("cname", cname)
process = request.POST.getlist('process[]')
print("process", process)

Django with Bootstrap 2nd Modal (or subsequent modals) not working

Using the Django for loop It am generating a paginated list from the database. This works. The generated list has a delete button to the right of each entry.
At the same page load I an using the the django for loop to generate individual Modal windows that match each delete button. So the button will call modal ID=### and there is a modal ### that matches. The HTML appears to render perfectly.
When I delete the TOP(First entry on the page) it works like a charm, I can do that all day long, with entries moving up and being deleted.
THE PROBLEM: When I choose a 2nd position or lower button the screen goes grey and freezes it needs a reload to respond again. This pattern is repeatable.
HTML:
{% load static %}
{% block content %}
<link rel="stylesheet" href="{% static "css/cabinet_table.css"%}">
<div class="col-lg-2">
</div>
<div class="col-lg-8">
<div class="table-responsive">
<table class="table table-hover">
<th colspan="3"style="text-align: center;"><h2>{{ user.username|capfirst }} Notes</h2></th>
{% for note in note_list %}
<tr>
<td >{{ note.title }}</td>
<td>{{ note.text|truncatewords:15 }}</td>
<td><button type="button" class="btn btn-info btn-success" data-toggle="modal" data-target="#deleteModal{{ note.id }}">Delete</button></td>
</tr>
{% endfor %}
</table>
</div>
<!-- Pagination below -->
{% if note_list.has_other_pages %}
<ul class="pagination">
{% if note_list.has_previous %}
<li>«</li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in note_list.paginator.page_range %}
{% if note_list.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li>{{ i }}</li>
{% endif %}
{% endfor %}
{% if note_list.has_next %}
<li>»</li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}
</div>
<div class="col-lg-2">
</div>
{% include 'cabinet/_note_list_modal.html' %}
{% endblock %}
Included HTML(The modal generation):
{% for note in note_list %}
<!-- Modal {{ note.id }} -->
<div class="modal fade" id="deleteModal{{ note.id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel{{ note.id }}">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel{{ note.id }}">Delete Note</h4>
</div>
<div class="modal-body">
<h4>Title :</h4> {{ note.title }}<br>
<h4>Idea:</h4> {{ note.text|truncatewords:25 }}
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-danger" onclick="settleUp{{ note.id }}()" >Delete</button>
</div>
</div>
</div>
</div>
<div id="hiddenForm{{ note.id }}" style="display: none" class="visibility=hidden">
<form class="" action="/fc/delete/{{ note.id }}" name="hiddenForm{{ note.id }}" method="post">
{% csrf_token %}
<input type="hidden" name="deleteNote{{ note.id }}" id="deleteNote{{ note.id }}" value="{{ note.id }}">
<!-- <button type="submit" name="betBalance">Update Balance</button> -->
</form>
<script type="text/javascript">
function settleUp{{ note.id }}(){
document.forms.hiddenForm{{ note.id }}.submit()
}
</script>
{% endfor %}
OBJECTIVE: Click on any delete button have its modal pop up and work.
Thanks for any help.
PS using inspect, which I don't know how to use well, I see no errors in the console.
The problem with this, which was solved in another question, is there is a missing DIV tag. Thanks for everyone who looked.

Django, form can not be submitted

I created the form, but i can not submit it using post method. I worked with generic views
When i try to submit it,this link appeares '?csrfmiddlewaretoken=4u6glHHJVueXItlN...'
here's my code:
form template:
<form action="" method="post" class="form-inline">
{% csrf_token %}
<div class="modal-body clearfix">
{% include 'words/word_form.html' %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
generic view:
class Create_Word(generic.CreateView):
model=Word
fields=['name','translation']
template_name = "words/list_all.html"
Here's word_form template:
{%load extra%}
{% for field in form %}
<div class="form-group ">
<div class="col-sm-10">
<span class="text-danger-small">{{ field.errors }}</span>
</div>
<label class="control-label col-sm-2 m-l-1 ">{{ field.label_tag }}</label>
<div class= "col-sm-9 m-b-1">
{{ field | addcss:"form-control" }}
</div>
{% endfor %}