简体   繁体   English

按下按钮时,如何使用带有新数据的 django 视图更新 firebase 记录?

[英]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我正在开发一个仪表板,管理员可以在其中检索存储在 firebase 中的所有数据,然后对这些数据执行操作

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.我已经设法使用 django 视图在 firebase 中发布数据,我也可以使用视图在 html 表中检索相同的数据,但发现很难用新数据更新这些数据。

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我的代码更新了 firebase 中的所有节点而不是只有一个节点 我知道我的问题是如何检索要更新的单个 id

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这是我试图用来更新 Firebase 的 javascript

   <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.我没有向您展示我的 firebase-real-time-database,因为它工作正常,我想在按下停用按钮时更新单个节点 ID,但不要一次更新所有节点。

Thanks for your help.谢谢你的帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该怎么办:“按下录制按钮后,该按钮将闪烁” - How can I do: “As the the record button is pressed, the button will be flashing” 按下按钮时如何模拟按下的不同按钮? - How can I simulate a different button pressed when a button is pressed? 当按下HTML按钮时,如何显示从PHP函数返回的数据 - How can I display data returned from a PHP function when a HTML button is pressed 如何记录按下的链接? - How can I record the link pressed? 在HTML上按下按钮后,如何重新创建重新加载问题 - How can I recreate the reload issue when a button is pressed on HTML 按下后退按钮时如何取消选择单选按钮? - How can I deselect radio buttons when back button is pressed? (龙卷风)如何在按下按钮时将参数传递给服务器? - (tornado)how can I pass argument to server when a button is pressed? 按下特定按钮时如何清除setInterval? - How can I clear setInterval when a specific button is pressed? 当主容器上有data-dismiss =&#39;modal&#39;并且按下按钮以展开内部面板时,如何保持模式不关闭 - how can i keep a modal from closing when there is a data-dismiss='modal' on the main container and when a button is pressed to expand panels inside 如何使用表单和视图保存 django 动态表单集数据 - How can i save django dynamic formset data using forms and views
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM