Django Graphos Chart Not Loaded On Table Update Using Ajax - django

I have been using django graphos and it works fine till i realize that the chart is not drawn when i update my table through ajax instead of a page refresh. Let me describe the issue further.
When my page is loaded for the first time, all the charts are loaded properly using {{chart.as_html}} . I am usng django by the way .
This charts are displayed in a table.
But when user selects different value from a dropdown and click reload, my code will only update the table through ajax instead of a whole page refresh. Unfortunately , when the table is updated, the chart with new data is not displayed. It just appears blank. I can see all the data from the developer tools but the chart just fail to appear.
I have checked through various topics but I am unable to get it working.
Part of my main html file
{% if objects %}
{% for obj in objects %}
<tr data-toggle="collapse" data-target="#{{obj.name}}" data-parent="#myGroup" class="accordion-toggle">
<!--tr>-->
<td style="border:0;"><img src='{{STATIC_URL}}/static/images/{{obj.name}}.png' class="infostockIcon"></td>
{% if obj.risk == "high" %}
{% if obj.dpr == "high" %}
<td class="tablerisk"><strong> * {{obj.name}} </strong></td>
{% else %}
<td class="tablerisk"><strong> {{obj.name}} </strong></td>
{% endif %}
<td class="tablerisk"> {{obj.stock_price}} </td>
<td class="tablerisk" style="color: black;"> {{obj.option_strategy}} </td>
<td class="tablerisk" style="color: black;"> {{obj.whiz_strategy}} </td>
<td class="tablerisk"> {{obj.strike_price}} </td>
<td class="tablerisk"> {{obj.expiry_date}} </td>
<td class="tablerisk"> {{obj.premium}} </td>
<td class="tablerisk"> {{obj.roi}} </td>
{% else %}
{% if obj.dpr == "high" %}
<td><strong> * {{obj.name}} </strong></td>
{% else %}
<td><strong> {{obj.name}} </strong></td>
{% endif %}
<td> {{obj.stock_price}} </td>
<td> {{obj.option_strategy}} </td>
<td> {{obj.whiz_strategy}} </td>
<td> {{obj.strike_price}} </td>
<td> {{obj.expiry_date}} </td>
<td> {{obj.premium}} </td>
<td> {{obj.roi}} </td>
{% endif %}
<td style="border:0;"><span class="caret black"></span></th></td>
</tr>
<tr>
<td style="border: 0;padding: 0 !important;"></td>
<td colspan="8" style="padding: 0 !important; border: none;">
<div class="accordion-body collapse" id="{{obj.name}}">
<ul id="tabs" class="nav nav-tabs" data-tabs="tabs">
<li data-toggle="tab" class="active">
Monthly Seasonality
</li>
<li data-toggle="tab">
Weekly Seasonality
</li>
<li data-toggle="tab">
Dividend
</li>
<li data-toggle="tab">
{{obj.name}} Info
</li>
</ul>
<div id="my-tab-content" class="tab-content">
<div class="tab-pane active" id="{{obj.name}}monthseason">
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<h4>Monthly Seasonality<img src='{{STATIC_URL}}/static/info.png' class="infoIcon" style="float: none; right: 0px; margin-left: 5px" /></h4>
<div id="gchart_div">
{{ obj.monthly_chart.as_html}}
</div>
</div>
</div>
</div>
<div class="tab-pane" id="{{obj.name}}weekseason">
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<h4>Weekly Seasonality<img src='{{STATIC_URL}}/static/info.png' class="infoIcon" style="float: none; right: 0px; margin-left: 5px"/></h4>
{{ obj.weekly_chart.as_html}}
</div>
</div>
</div>
<div class="tab-pane" id="{{obj.name}}dividend">
<!--div style="padding-left: 2%;" >-->
<div class="row">
<div class="col-md-5 col-xs-6 col-sm-6 alignLeft">
<label >TTM Dividend Yield:</label>
<label >{{obj.ttm_dividend_yield}}</label>
</div>
<div class="col-md-5 col-xs-6 col-sm-6 alignLeft">
<label >Ex-Dividend Date:</label>
<label >{{obj.ex_div}}</label>
</div>
</div>
<!--/div>-->
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<h4>Dividend Yield<img src='{{STATIC_URL}}/static/info.png' class="infoIcon" style="float: none; right: 0px; margin-left: 5px"/></h4>
{{obj.div_yearly_chart.as_html}}
</div>
</div>
</div>
<div class="tab-pane alignRight" id="{{obj.name}}data">
<div style="padding-left: 2%;" >
<div class="row">
<div class="col-md-5 col-xs-6 col-sm-6 alignLeft">
<label >Sector:</label>
<label >{{obj.sector}}</label>
</div>
<div class="col-md-5 col-xs-6 col-sm-6 alignLeft">
<label >Industry:</label>
<label >{{obj.industry}}</label>
</div>
</div>
<div class="row">
<div class="col-md-5 col-xs-6 col-sm-6 alignLeft">
<label >Earning Date:</label>
<label >{{obj.earnings_date}}</label>
</div>
<div class="col-md-5 col-xs-6 col-sm-6 alignLeft">
<label >Ex-Dividend Date:</label>
<label >{{obj.ex_div}}</label>
</div>
</div>
<div class="row">
<div class="col-md-5 col-xs-6 col-sm-6 alignLeft">
<label >52 Week Range:</label>
<label >{{obj.year_range}}</label>
</div>
<div class="col-md-5 col-xs-6 col-sm-6 alignLeft">
<label >5 Year Low:</label>
<label >{{obj.five_year_low}}</label>
</div>
</div>
<div class="row">
<div class="col-md-5 col-xs-6 col-sm-6 alignLeft">
<label >DPR:</label>
<label >{{obj.dpr_value}}</label>
</div>
<div class="col-md-5 col-xs-6 col-sm-6 alignLeft">
<label >Latest News:</label>
<a href={{obj.url}}>Seeking Alpha</a>
</div>
</div>
</div>
<br>
</div>
</div>
</div>
</td>
</tr>
My ajax call:
`var $myGroup = $('#myGroup');
$myGroup.on('show.bs.collapse','.collapse', function() {
$myGroup.find('.collapse.in').collapse('hide');
});
$('#tabs li a').click(function (e) {
$('#tabs li a.active').removeClass('active');
$(this).parent('li a').addClass('active');
});
$("#loadprofit").on("click", function(e)
{
//alert("asdas");
e.preventDefault();
var selectedOption = $("#strategy").val();
var selectedDate = $("#strategy2").val();
csrf_token = "{{ csrf_token }}";
$.ajax({
method: "POST",
url: window.location.href,
data: { selected_option: selectedOption,selected_date:selectedDate,csrfmiddlewaretoken: csrf_token },
})
.done(function( msg )
{
console.log(msg);
$("#sct tbody").html(msg);
});
});
</script>
{% endblock %}
`
This whole table gets updated when user clicks on a button. Then it calls views.py and table gets updated with the latest value. But {{ obj.monthly_chart.as_html}} is not loaded and it appears blank although i can see the whole chart drawing function and the data form the developer tool
Any help is greatly appreciated.

Related

Need support, Initially Django Search page is displaying all data, while i want to have only search text boxes and display data after user input

Views.py
def advance_filter(request):
user_list = SarnarLogs.objects.all()
user_filter = UserFilter(request.GET)
return render(request, 'advance_filter.html', {'filter': user_filter})
Template: Advance_filter.html
<form method="get">
<div class="well">
<h4 style="margin-top: 0">Filter</h4>
<div class="row">
<div class="form-group col-sm-4 col-md-3">
{{ filter.form.source.label_tag }}
{% render_field filter.form.source class="searchfield" %}
</div>
<div class="form-group col-sm-4 col-md-3">
{{ filter.form.destination.label_tag }}
{% render_field filter.form.destination class="searchfield" %}
</div>
<div class="form-group col-sm-4 col-md-3">
{{ filter.form.port.label_tag }}
{% render_field filter.form.port class="searchfield" %}
</div>
<div class="form-group col-sm-4 col-md-3">
{{ filter.form.request_no.label_tag }}
{% render_field filter.form.request_no class="searchfield" %}
</div>
</div>
<button type="submit" class="btn btn-primary" placeholder="Search" aria-label="Search" name="q" value='{{ request.GET.q }}'>
<span class="glyphicon glyphicon-search"></span> Search
</button>
</div>
</form><br>
<div class="col-12">
<div class="card my-4">
<div class="card-header p-0 position-relative mt-n4 mx-3 z-index-2">
<div class="bg-gradient-primary shadow-primary border-radius-lg pt-4 pb-3">
<h6 class="text-white text-capitalize ps-3">HUB Requests</h6>
</div>
</div>
<div class="card-body px-0 pb-2">
<div class="table-responsive p-0">
<table class="table align-items-center mb-0">
<thead>
<tr>
<th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">ID</th>
<th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7 ps-2">SOURCE</th>
<th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">DESTINATION</th>
<th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">PORT</th>
<th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">REQUEST TYPE</th>
<th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">REQUEST_NO</th>
<th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">REQUESTER</th>
<th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">START DATE</th>
<th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">EXPIRY DATE</th>
<th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">Days Left</th>
<th class="text-secondary opacity-7"></th>
</tr>
</thead>
<tbody>
{% for user in filter.qs %}
<tr>
<td>
<div class="d-flex px-2 py-1">
<div class="d-flex flex-column justify-content-center">
<h6 class="mb-0 text-sm">{{user.id}}</h6>
</div>
</div>
</td>
<td>
<p class="text-xs font-weight-bold mb-0">{{user.source}}</p>
</td>
<td class="align-middle text-center text-sm">
<p class="text-xs font-weight-bold mb-0">{{user.destination}}</p>
</td>
<td class="align-middle text-center text-sm">
<p class="text-xs font-weight-bold mb-0">{{user.port}}</p>
</td>
<td class="align-middle text-center text-sm">
<p class="text-xs font-weight-bold mb-0">{{user.request_type}}</p>
</td>
<td class="align-middle text-center text-sm">
<p class="text-xs font-weight-bold mb-0">{{user.request_no}}</p>
</td>
<td class="align-middle text-center text-sm">
<p class="text-xs font-weight-bold mb-0">{{user.requester}}</p>
</td>
<td class="align-middle text-center">
<span class="text-secondary text-xs font-weight-bold">{{user.start_date|date:'d-m-Y'}}</span>
</td>
<td class="align-middle text-center">
<span class="text-secondary text-xs font-weight-bold">{{user.expiry_date|date:'d-m-Y'}}</span>
</td>
<td class="align-middle text-center">
<span class="text-secondary text-xs font-weight-bold">{{ user.expiry_date|timeuntil:user.start_date }}</span>
</td>
</tr>
{% empty %}
<tr>
<td colspan="5">No data</td>
</tr>
{% endfor %}
</tbody>
Any solution to load the page with search boxes and later when user provide inputs and hits the seach to display the data. Problem is that there are millions of records and page is consuming time to load up.
Appriciate if someone can provide insight in resolving it.

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>
?

URL automatically changes & it gives me the error of Page not found

"127.0.0.1:8000/adminpanel/edit_gallary/29"
So this is the main problem when I first edit it works properly. But when I want to edit the second time URL automatically changes & it changes this way.
"127.0.0.1:8000/adminpanel/edit_gallary/edit_gallary/29"
This is my views.py file:
def edit_gallary(request,gallaryid):
table = gallary.objects.get(id=gallaryid)
gallary_form = gallaryform(instance=table)
if request.method=='POST':
form = gallaryform(data=request.POST, files=request.FILES, instance=table)
if form.is_valid():
form.save()
table=gallary.objects.all()
return render(request,"gallary1.html",{'table':table})
context = {'gallary_form':gallary_form}
return render(request,'edit_gallary.html',context)
This is my URLS.py file:
path('edit_gallary/<int:gallaryid>',views.edit_gallary,name="edit gallery"),
This is my edit_gallary.html file:
{% extends 'index.html' %}
{% block content %}
<body>
<div class="main-wrapper">
<div class="app" id="app">
<header class="header">
<div class="header-block header-block-collapse d-lg-none d-xl-none">
<button class="collapse-btn" id="sidebar-collapse-btn">
<i class="fa fa-bars"></i>
</button>
</div>
<div class="header-block header-block-nav">
<ul class="nav-profile">
<li class="profile dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button"
aria-haspopup="true" aria-expanded="false">
<div class="img"
style="background-image: url('https://avatars3.githubusercontent.com/u/3959008?v=3&s=40')">
</div>
<span class="name">Admin</span>
</a>
<div class="dropdown-menu profile-dropdown-menu" aria-labelledby="dropdownMenu1">
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">
<i class="fa fa-power-off icon"></i> Logout </a>
</div>
</li>
</ul>
</div>
</header>
<aside class="sidebar">
<div class="sidebar-container">
<div class="sidebar-header">
<div class="brand">
<div class="logo">
<span class="l l1"></span>
<span class="l l2"></span>
<span class="l l3"></span>
<span class="l l4"></span>
<span class="l l5"></span>
</div> Compare school
</div>
</div>
<nav class="menu">
<ul class="sidebar-menu metismenu" id="sidebar-menu">
<li>
<a href="insert_school">
<i class="fa fa-home"></i>Back</a>
</li>
</ul>
</nav>
</div>
</aside>
<div class="sidebar-overlay" id="sidebar-overlay"></div>
<div class="sidebar-mobile-menu-handle" id="sidebar-mobile-menu-handle"></div>
<div class="mobile-menu-handle"></div>
<article class="content responsive-tables-page">
<div class="title-block">
<h1 class="title">Gallary</h1>
<p class="title-description"></p>
</div>
<section class="section">
<div class="row">
<div class="table-responsive">
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<table class="table table-striped table-bordered">
{{ gallary_form }}
<tr>
<td class="text-right mb-3">
<input type="submit" value="register" class="btn btn-primary" >
</td>
</tr>
</table>
</form>
</div>
</div>
</section>
</article>
</div>
</div>
<script src="js/vendor.js"></script>
<script src="js/app.js"></script>
{% endblock %}
</body>
It seems you need to change 'edit_gallary/<int:gallaryid>' to '<int:gallaryid>/edit_gallary/' in urls.py or just try adding a slash to the route end in your path

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)

multi modals bootstrap in for loop 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 %}