1

I am making my first application with Django. The goal is to have a table that displays the current inventory of laboratory reagents. Once a reagent is taken out of storage a user will be able to hit a subtract button to subtract from the current inventory. Currently I have a table that displays the reagent name and quantity. I would like to add a column to the table that has a subtract button that will subtract one from the corresponding reagents current inventory.

Can anyone please give me some suggestions on how to implement a subtract button? Any help would be much appreciated.

models.py
class Reagentquant(models.Model):

    Name_Of_Reagent= models.TextField()
    Reagent_Quantity= models.TextField()
tables.py

class ReagentquantTable(tables.Table):
    class Meta:
        model = Reagentquant
        template_name = 'django_tables2/bootstrap4.html'
        row_attrs = {'reagent_id': 'reagent'}
    edit= TemplateColumn(template_name='inventory/update_column.html')
views.py

def reagentquant(request):
    table = ReagentquantTable(Reagentquant.objects.all())
    RequestConfig(request).configure(table)
    return render(request, 'inventory/quant.html', {'table': table})


class RemoveReagentView:
    def post(self, request, *args, **kwargs):
        reagent_id = request.POST['reagent_id']
        reagent = Reagentquant.objects.filter(id=reagent_id)

        reagent.update(Reagent_Quantity=F('Reagent_Quantity')-1)
        data = {"count": reagent.Reagent_Quantity}
        return JsonResponse(data)
template 

<a class="btn btn-info btn-sm">-</a>

<script type="text/javascript">

  $(document).on('click', 'odd', function (e) {  // Where `element_with_reagentID_class` is the common classname of ALL elements that have your `reagent_id` inserted into them
      e.preventDefault();

      var $this = $(this);
      var reagent_id = $this.attr('reagent')  // Where `reagent_id` is whatever the attribute is that you added

      $.ajax({
          type: "POST",
          url: "{% url 'inventory-quant' %}",  // Where `reagent_urlname` is the name of your URL specified in urls.py
          data: {reagent_id: reagent_id, csrfmiddlewaretoken: "{{ csrf_token }}"}
      })
      .done(function (response){
          console.log(response)
      })
  })}


</script>

/script>
</td>

                    </tr>



                    <tr scope="row" reagent_id="reagent" class="odd">

                            <td >17</td>

                            <td >CRP</td>

                            <td >22</td>

                            <td >

<a class="btn btn-info btn-sm" onClick="testMe()">-</a>

<script type="text/javascript">

  $(document).on('click', 'reagent', function (e) {  // Where `element_with_reagentID_class` is the common classname of ALL elements that have your `reagent_id` inserted into them
      e.preventDefault();

      var $this = $(this);
      var reagent_id = $this.attr('reagent')  // Where `reagent_id` is whatever the attribute is that you added

      $.ajax({
          type: "POST",
          url: "/quant",  // Where `reagent_urlname` is the name of your URL specified in urls.py
          data: {reagent_id: reagent_id, csrfmiddlewaretoken: "NjfgIlWzzSBwq8EJh5jsdIqns4tK6tMFLH4vEkUSWAHW5VCHigpVpmzkDPBwbyL3"}
      })
      .done(function (response){
          console.log(response)
      })
  })}


</script>
</td>

                    </tr>



                    <tr scope="row" reagent_id="reagent" class="even">

                            <td >20</td>

                            <td >MTX</td>

                            <td >22</td>

                            <td >

1 Answers1

0

To update the quantity, you could do something like this:

class RemoveReagentView(View):
    def post(self, request, *args, **kwargs):
        reagent_id = request.POST['reagent_id']
        reagent = Reagentquant.objects.filter(id=reagent_id)

        reagent.update(Reagent_Quantity=F('Reagent_Quantity')-1)
        data = {"count": reagent.Reagent_Quantity}
        return JsonResponse(data)

The next step would be hooking it into the django_tables2 rendering. Your best bet would be to add a custom row attribute of the reagent.id value, capture it via Javascript, and then use an AJAX POST request to call the RemoveReagentView() view I specified above. You would obviously need a +/- button as well, which it seems you can add using something along the lines of this answer.

Overall it will take a bit of clever hacking around, but it seems very doable.

edit

Here is an example AJAX request:

$(document).on('click', '.element_with_reagentID_class', function (e) {  // Where `element_with_reagentID_class` is the common classname of ALL elements that have your `reagent_id` inserted into them 
    e.preventDefault();
    
    var $this = $(this);
    var reagent_id = $this.attr('reagent_id')  // Where `reagent_id` is whatever the attribute is that you added

    $.ajax({
        type: "POST",
        url: "{% url 'reagent_urlname' %}",  // Where `reagent_urlname` is the name of your URL specified in urls.py
        data: {reagent_id: reagent_id, csrfmiddlewaretoken: "{{ csrf_token }}"} 
    })
    .done(function (response){
        console.log(response)
    })
})
Community
  • 1
  • 1
Hybrid
  • 6,741
  • 3
  • 25
  • 45
  • Thank you so much for your response. I was able to add the custom row attribute and the subtract button but now I am running into issues with a AJAX POST request. Would you be able to point me in the right direction to call the RemoveReagentView in the AJAX POST request. Thanks again. – Brandon Cavinee Apr 26 '19 at 00:09
  • I updated my post to show you an example. Please note that I changed the view to use `POST` instead of `GET` now as well. – Hybrid Apr 26 '19 at 00:20
  • I will try this tomorrow thank you. Also I should have mentioned this before. I am generating my table from a SQlite database. Will this effect that AJAX request? – Brandon Cavinee Apr 26 '19 at 01:56
  • Probably not, but you will know for sure by testing – Hybrid Apr 26 '19 at 02:10
  • I am still not getting a response when I click the "-" button. I edited my original post with all the updates I have made with your help so far. I also included a snippet of the page source of my current table. Any suggestions would be greatly appreciated. – Brandon Cavinee Apr 26 '19 at 21:37