0

我正在尝试将 Django 网页中的数据上传到 Firebase 实时数据库。但我收到错误:

'/post_create/'localid 处的 KeyError'

到目前为止,我已经完成了 firebase 身份验证、登录凭据和注销凭据。但这是我卡住的数据上传部分。这是我的代码:views.py

        from django.shortcuts import render
        from django.contrib import auth
        import pyrebase
        
        config={
        
            'apiKey': "AIzaSyDWBmMMaF3YJO7ZxMXgbBRzqw7hqe564vc",
            'databaseURL': '',
            'authDomain': "cpanel-103d6.firebaseapp.com",
            'projectId': "cpanel-103d6",
            'storageBucket': "cpanel-103d6.appspot.com",
            'messagingSenderId': "489072871004",
            'appId': "1:489072871004:web:fd717d9bd6d0a4007f4899"
        
        }
        
        
        firebase= pyrebase.initialize_app(config)
        database= firebase.database()
        authe =firebase.auth()

    def postsign(request):
        email= request.POST.get('email')
        passw = request.POST.get('pass')
    
        try:
            user = authe.sign_in_with_email_and_password(email, passw)
        except:
            message = 'invalid credentials'
            return render(request, 'signin.html', {'messg': message})
    
        print(user['idToken'])
        session_id=user['idToken']
        request.session['uid']=str(session_id)
        return render(request, 'welcome.html', {'e': email})
        def create(request):
    return render(request, 'create.html')

def post_create(request):
    import time
    from datetime import datetime,timezone
    import pytz

    tz=pytz.timezone('Asia/Kolkata')
    time_now=datetime.now(timezone.utc).astimezone(tz)
    millis= int(time.mktime(time_now.timetuple()))
    print('mili'+str(millis))
    work= request.POST.get('work')
    progress= request.POST.get('progress')
    idtoken=request.session['uid']
    a= authe.get_account_info(idtoken)
    a= a['users']
    a= a[0]
    a= a['localid']
    print("info"+str(a))
    data={"work":work,
          "progress": progress

          }
    database.child('users').child(a).child('reports').child('millis').set(data)
    return render(request)

网址.py:

from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
    path('admin/', views.signin),
    path('postsign/', views.postsign),
    path('logout/', views.logout, name='log'),
    path('create/', views.create,name='create'),
    path('post_create/', views.post_create,name='post_create')
]

创建.html:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">


</head>
<body>
  <h1><b>Upload Documents Here</b></h1>
  <form action="/post_create/" method="post">
      {% csrf_token %}
      Work Assigned: <input type = "text" name="work" required>
      Progress:<textarea rows="10" cols="50" name="progress" required ></textarea>
      <input type="submit" value="SUBMIT">
      <br><br> <button type="button" onclick="location.href={% url 'log' %}"> LOGOUT </button>


  </form>


</body>
</html>

为了创建这个项目,我参考了这个 youtube 视频。 https://www.youtube.com/watch?v=bq0AszeDZf4&t=53s

4

0 回答 0