Python 我想给匿名用户一些令牌并存储他的数据,如果用户注册了,后端应该传输数据

Python 我想给匿名用户一些令牌并存储他的数据,如果用户注册了,后端应该传输数据,python,django,vue.js,Python,Django,Vue.js,这是在后端生成的“令牌” def get(self, request): if self.user.is_authenticated: # the logged user's data is stored else: token = get_random_string() #this function was imported from other file 在这里,我将令牌作为Json文件发送到请求的url 但是,当用户被请求访问ur

这是在后端生成的“令牌”

    def get(self, request):
    if self.user.is_authenticated:
        # the logged user's data is stored

    else:
        token = get_random_string() #this function was imported from other file
在这里,我将令牌作为Json文件发送到请求的url 但是,当用户被请求访问url时,不能将其放在前端并发送到后端 在用户已经请求之前! 请帮帮我!!! 这是前端的Vue文件

searchProduct(keyword){
 if(this.selectedCatId != '' && this.selectedCatId != 'all'){
   this.$router.push({name: 'search', query:{
     k:keyword,
     cpn: this.$route.query.cpn ? this.$route.query.cpn : 1,
     cid: this.selectedCatId}
    })
 }else{
   this.$router.push({name: 'search', query:{
     k: keyword,
     cpn: this.$route.query.cpn ? this.$route.query.cpn : 1}
    })
 }
 this.$axios.get('requested url'+this.searchKeyword+'&pagenum=1&productsPerPage=10', {
    params: {
      usertoken: localStorage.getItem('unregUserSearchToken')
    }
 })
  .then(function (response) {
    localStorage.setItem('unregUserSearchToken', response.data.search_token)
    console.log(response);
  })
searchProduct(关键字){
this.$axios.get('请求的url'+this.searchKeyword+'&pagenum=1&productsPerPage=10'{
参数:{
usertoken:localStorage.getItem('unregUserSearchToken')
}
}).然后(功能(响应){
localStorage.setItem('unregUserSearchToken',response.data.search\u token)
控制台日志(响应);
如果(this.selectedCatId!=''&&this.selectedCatId!=='all'){
这是$router.push({
名称:'搜索',
查询:{
k:关键字,
cpn:this.$route.query.cpn?this.$route.query.cpn:1,
cid:这是selectedCatId
}
})
}否则{
这是$router.push({
名称:'搜索',
查询:{
k:关键字,
cpn:this.$route.query.cpn?this.$route.query.cpn:1
}
})
}
})
}

在继续路由跳转操作之前,您需要等待http请求响应,以确保在新页面中正确检索存储缓存。

当用户搜索产品请求后端同时发生两次时,我尝试了,但无法到达目标,结果是将数据保存到同一用户并生成新的令牌创建第二个对象。您应该将推送路由操作放在请求成功中callback@sugars什么意思?你能告诉我你想在答案栏说什么吗?