我试图从第三方网站的API存储一个会话ID和一个访问令牌到我的用户配置文件我一直在尝试几天和搜索,但我找不到这样的东西
我试图导入配置文件示例,以便我可以将数据保存到用户配置文件示例,但我没有找到如何,这是我的代码
#views.py
from bs4 import BeautifulSoup
import xml.etree.ElementTree as ET
from django.contrib.auth.decorators import login_required
import urllib.parse
from django.views.generic import View
from users.models import Profile
from django.contrib.auth.models import User
def profile_view(request):
RuName= 'driss_aitkassi-drissait-crm-SB-ijuzzy'
r= {
'RuName': RuName
}
response = api.execute('GetSessionID', r)
print(response)
res =response.text
tree= ET.fromstring(res)
#here is my session id from the website's api
for node in tree.iter('{urn:ebay:apis:eBLBaseComponents}GetSessionIDResponse'):
SessionID= node.find('{urn:ebay:apis:eBLBaseComponents}SessionID').text
#i tried this :
s=Profile(session_id=SessionID)
s.save()
# and also tried this
# and also this:
Profile.objects.update(session_id=SessionID)
Profile.objects.model.save()
下面是Models.py文件:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User,related_name='profile' , on_delete=models.CASCADE)
token = models.TextField(max_length=500, blank=True)
session_id=models.TextField( max_length=500, blank=True)
def __str__(self):
return self.user.username
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
请帮我这个或只是指出我在正确的方向,谢谢
1条答案
按热度按时间zujrkrfu1#
如果我理解正确的话,您正在尝试将数据保存到每个用户会话。
您可以使用Django的sessions存储。
但是如果你想保存来自不同会话的请求所共有的数据,可以尝试Django的cache framework。