'Django table update using Ajax
I want to update my django template data using Ajax. I want to let the users update the status from the dropdown and it should reflect without the page being refreshed Please refer this
Please help, i am stuck into this
Django template
{% for vulnerability in vulnerabilitys %}
<tr>
<th scope="row" >{{vulnerability.id}}A{{vulnerability.sNo}}</th>
<td>{{vulnerability.vul_title}}</td>
<td>{% for asset in vulnerability.asset.all %}{{asset}}<br>{% endfor %}</td>
<td><span
{% if vulnerability.severity|lower == 'info' %} class="badge rounded-pill bg-info"
{% elif vulnerability.severity|lower == 'high' %} class="badge rounded-pill bg-danger"
{% endif %}>
{{vulnerability.severity}}</span></td>
<td>{{vulnerability.vul_url}}</td>
<td>{{vulnerability.last_seen|date:'d-m-Y H:i'}} <i class='fas fa-clock' style='color:rgb(48, 75, 228)'></i></td>
<td><select id="ab" data-item = {{vulnerability.id}} qty-item="{{vulnerability.status}}" class="update" onchange="if (this.selectedIndex) doSomething();">
{% if vulnerability.status == 'Open' %}
<option value="{{ vulnerability.status }}">{{ vulnerability.status }}</option>
<option value="InProgress">InProgress</option>
<option value="Fixed">Fixed</option>
</select>
{% elif vulnerability.status == 'InProgress' %}
<option value="{{ vulnerability.status }}">{{ vulnerability.status }}</option>
<option value="Open">Open</option>
<option value="Fixed">Fixed</option>
</select>
{% else %}
<option value="{{ vulnerability.status }}">{{ vulnerability.status }}</option>
<option value="InProgress">InProgress</option>
<option value="Open">Open</option>
</select>
{% endif %}
<h5>{{vulnerability.status}}</h5></td>
</tr>{% endfor %}
script in the template
$(document).on('change', '.update', function(){
// var item = $(this).closest('tr');
// editItem(item);
var pid = $(this).attr('data-item');
var stat = $(this).attr('qty-item');
var x = document.getElementById("ab").value;
console.log("OLD",stat);
console.log("NEW",x)
var elem = document.getElementById('myTable2');
elem.style.color = 'green';
$.ajax({
url:'/statUpdate/',
data:{'pid': pid, 'stat':stat, 'x':x},
// dataType:'json',
success: function(data){
// $('h5').html(data);
}
});
});
views.py
def vul_update(request):
if (request.session['namee'] == 'none'):
return redirect(home)
username = request.session['namee']
pid = request.GET['pid']
stat = request.GET['stat']
x = request.GET['x']
print(pid)
print(stat)
vulnerabilities.objects.filter(id = pid).update(status = x)
a= vulnerabilities.objects.filter(id = pid)
print(a[0].status)
vulnerability = vulnerabilities.objects.filter(organisation__name = username)
context = {'username': username, 'vulnerabilitys': vulnerability}
# t = render_to_string('ajax/vul.html', context)
# return JsonResponse({'data':t})
success = a[0].status
return HttpResponse(success)
It is not working properly, please tell me what am I doing wrong? I want to let the client update the status and it should be updated in the django models also without the page being refreshed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
