【问题标题】:Passing and using a value from another method传递和使用另一个方法的值
【发布时间】:2016-03-10 11:04:03
【问题描述】:

在我看来.py;

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

在同一个view.py中,但功能不同,我需要使用author_name。如果我再写"author_name = request.GET.get('author')",它会返回“NONE”。

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

您能帮我如何将值从 get_social_data() 传递给 profile_export()。或者有没有其他方法可以获取 author_name?

谢谢。

【问题讨论】:

  • 但是我不想将 profile_export 调用到 get_social_data 中,因为这些视图是通过单击不同的按钮来调用的。调用 get_social_data 来查看用户的个人资料。但是,profile_export() 允许用户将该用户的所有信息导出到 Excel 文件中。
  • 你不知道,视图是对请求的返回。您将作者作为获取参数传递
  • 每次调用视图都是一个不同的请求。如果要从视图中的请求中获取参数,则需要在请求中实际包含这些参数。
  • 如果是相同的author,您可以将其保存在第一个函数的session 中,然后在第二个函数的session 中使用它。
  • 这正是我需要的@doru。谢谢它解决了我的问题。

标签: django django-views


【解决方案1】:

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

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

在第二个函数中;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-02
    • 2012-01-07
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    相关资源
    最近更新 更多