传递并使用另一种方法的值 [英] Passing and using a value from another method

查看:225
本文介绍了传递并使用另一种方法的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的view.py;

  def get_social_data(request):
author_name = request.GET.get ('author')
//做某事
return HttpResponse(json.dumps(result),content_type ='application / json')

在同一个view.py中,但在不同的功能中,我需要使用author_name。如果我再次写入author_name = request.GET.get('author'),则返回NONE。



$ $ $ $ $ $ $ $ $ def $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

你能帮助我如何从get_social_data()将值传递给profile_export()。还是有其他方法来获取author_name?



谢谢。

解决方案

如果是相同的 author 您可以将其保存在django中第一个函数中的 session ,然后在第二个函数的会话中使用它。


  def get_social_data(request):
author_name = request.GET.get('author')
session [' author_name'] = author_name
.....

在第二个函数中; / p>

  def profile_export(request,slug):
author_name = session ['author_name']


In my view.py;

def get_social_data(request):
    author_name = request.GET.get('author')
    // To do something
    return HttpResponse(json.dumps(result), content_type='application/json')

In the same view.py but in different function, I need to use author_name. If I again write "author_name = request.GET.get('author')", it returns "NONE".

def profile_export(request, slug):
     author_name = request.GET.get('author') // Now NONE.

Can you help me how to pass the value to profile_export() from get_social_data(). Or is there any other way to get author_name?

Thank you.

解决方案

If is the same author you can save it in a django session in the first function and then use it from the session in the second function.

def get_social_data(request):
    author_name = request.GET.get('author')
    session['author_name'] = author_name
    .....

And in the second function;

def profile_export(request, slug):
    author_name = session['author_name']

这篇关于传递并使用另一种方法的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆