簡體   English   中英

按下按鈕時,如何使用帶有新數據的 django 視圖更新 firebase 記錄?

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

我正在開發一個儀表板,管理員可以在其中檢索存儲在 firebase 中的所有數據,然后對這些數據執行操作

我已經設法使用 django 視圖在 firebase 中發布數據,我也可以使用視圖在 html 表中檢索相同的數據,但發現很難用新數據更新這些數據。

我的代碼更新了 firebase 中的所有節點而不是只有一個節點 我知道我的問題是如何檢索要更新的單個 id

我已經嘗試了幾種在線提供的解決方案,例如這個,但問題仍然存在

 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')

這是我試圖用來更新 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;
  }

我沒有向您展示我的 firebase-real-time-database,因為它工作正常,我想在按下停用按鈕時更新單個節點 ID,但不要一次更新所有節點。

謝謝你的幫助。

我通過將 firebase Django 更改為 nodejs 並將其連接到 firebase 解決了這個問題,這使得使用 jQuery 進行數據檢索變得非常容易和引人入勝,而不是使用 Django 調用 firebase。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM