I am working on a dashboard where an admin can be able to retrieve all the data stored in firebase and then perform operations on this data

I have managed to post data in firebase using a django view and i can also retrieve the same data in an html table using a view but finding difficult to update this data with new data.

My code that updates all the nodes in the firebase instead of only one node i know my problem is on how to retrieve a single id to be updated

i have tried several solutions given online such as this one, but the problem has persisted

 this is my deactivate_status.html
    {% extends 'sidebar.html' %}
   {% block body %}

       <div class="container">
           <h4>Home<b> ></b> Deactivated Service Providers</h4>

              <div class="row">
                    <form id="form" method="POST" action = "/deactivate_status">
                                                {% csrf_token %}
                        <table class="table" border="0.5">

                                 <thead>
                                    <tr style="font-size:larger">

                                        <th>Username</th>
                                        <th>Telephone</th>
                                        <th>Profile Pic</th>
                                        <th>Service</th>
                                        <th>Status</th>
                                        <th>Take Action</th>


                                    </tr>
                                </thead>
                                <tbody>
                                     <tr style="font-size:large">
                                        {% for username,telephone,photo,service,status  in combine %}
                                             <td>{{username}}</td>
                                             <td>{{telephone}}</td>
                                             <div class="container1">
                                                 <td><img src="{{photo}}" style="max-width:20%; max-height:20%;cursor:pointer"
                                               onclick="onClick(this)" class="modal-hover-opacity"></td>
                                             </div>
                                             <td>{{service}}</td>
                                             <td>{{status}}</td>
                                             <td> <button class="btn-success" id="try" value="Deactivate" onClick="Reactivate()" name="status">Reactivate</button></tr>
                                        {% endfor %}
                                </tbody>
                        </table>

                    </form>
              </div>
       </div>


<!-- The Modal -->
<div id="modal01" class="modal" onclick="this.style.display='none'">
  <span class="close">&times;&nbsp;&nbsp;&nbsp;&nbsp;</span>
  <div class="modal-content">
    <img id="img01" style="max-width:100%">
  </div>
</div>
   {% endblock %}

        my view.py
        def deactivate_status(request):
            all_ids = 
        db.child('Users').child('AllServiceProviders').shallow().get().val()
        StatusIds = []
        for status_id in all_ids:
            StatusIds.append(status_id)
        AccStatus = []
        for i in StatusIds:
            accstate = db.child('Users').child('AllServiceProviders').shallow().child(i).child('accountStatus').get()
            accstate = accstate.val()
            if accstate=='approved':
                data={"accountStatus":"deactivated"}
                dat=db.child('Users').child('AllServiceProviders').child(i).update(data)
                AccStatus.append(dat)
        return redirect('index')

Here is my javascript that am trying to use to update the firebase

   <script>
    var config ={<!-- this is the configuration script that connects to 
 the firebase-->
    'apiKey': "AIzaSyBRoDeZjRdh9KpSm0BvaI33WQVpSlzEJXs",
     'authDomain': "canondashoard.firebaseapp.com",
      'databaseURL': "https://canondashoard.firebaseio.com",
      'projectId': "canondashoard",
      'storageBucket': "canondashoard.appspot.com",
      'messagingSenderId': "632859978103",
      'appId': "1:632859978103:web:9e081934e5446e3a"

     };
      firebase.initializeApp(config);
      function Reactivate() {
      var database = firebase.database();
      var stateref=database.ref().child('Users').child('AllServiceProviders');
      var key = stateref.push().key;
      var update ={};
      update[key]={accountStatus:'approved'};
      var result=stateref.update(update);
      document.getElementById('try').innerHTML='result:'+result;
  }

i have not shown you my firebase-real-time-database because it is working fine, i want to update a single node id when i press a deactivate button but not updating all of the nodes at once.

Thanks for your help.

0 Answers