简体   繁体   中英

How can i update firebase record using django views with new data when a button is pressed?

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': "XXXXXXXXXXXXXXXXXXXXX",
     'authDomain': "XXXXXXXXXXXXXXXXXXXXX",
      'databaseURL': "XXXXXXXXXXXXXXXXXXXXXXXXXXX",
      'projectId': "canondashoard",
      'storageBucket': "XXXXXXXXXXXXXXXXXXXXXX",
      'messagingSenderId': "XXXXXXXXXXXXXXXXXXXXXXXXXX",
      'appId': "XXXXXXXXXXXXXXXXXXXXXXXXX"

     };
      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.

我通过将 firebase Django 更改为 nodejs 并将其连接到 firebase 解决了这个问题,这使得使用 jQuery 进行数据检索变得非常容易和引人入胜,而不是使用 Django 调用 firebase。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM