【问题标题】:What is the difference between context_dict and context in django, python?django,python中的context_dict和context有什么区别?
【发布时间】:2015-02-15 02:32:20
【问题描述】:

在我遇到这种观点之前,我一直在我的方法中使用上下文:

def index(request):
    context = RequestContext(request)

    top_category_list = Category.objects.order_by('-likes')[:5]

    for category in top_category_list:
        category.url = encode_url(category.name)

    context_dict = {'categories': top_category_list}

    cat_list = get_category_list()
    context_dict['cat_list'] = cat_list

    page_list = Page.objects.order_by('-views')[:5]
    context_dict['pages'] = page_list

    if request.session.get('last_visit'):
    # The session has a value for the last visit
        last_visit_time = request.session.get('last_visit')

        visits = request.session.get('visits', 0)

        if (datetime.now() - datetime.strptime(last_visit_time[:-7], "%Y-%m-%d %H:%M:%S")).days > 0:
            request.session['visits'] = visits + 1
    else:
        # The get returns None, and the session does not have a value for the last visit.
        request.session['last_visit'] = str(datetime.now())
        request.session['visits'] = 1

    # Render and return the rendered response back to the user.
    return render_to_response('rango/index.html', context_dict, context) 

在上面的函数中有context_dict和context?这是为什么呢?

两者之间也有区别: context_dict = {'categories': top_category_list} 和 context_dict['categories'] = top_category_list

或者这完全一样?

谢谢你们!

【问题讨论】:

  • 这段代码工作正常吗?
  • 这段代码来自下面的教程,尽管我的索引视图与那个略有不同,因为这让我完全混淆了这些 context_dict 和上下文,所以不确定这段代码是否真的有效

标签: python django


【解决方案1】:
  • context_dict 是一个简单的字典

  • context是一个实例或RequestContext

render_to_response() 内部 context_dict 被(临时)添加到 context 实例中

代码(在这种情况下)可以写得更清楚(恕我直言):

def index(request):
    top_category_list = Category.objects.order_by('-likes')[:5]
    for category in top_category_list:
        category.url = encode_url(category.name)

    page_list = Page.objects.order_by('-views')[:5]
    cat_list = get_category_list()

    context_dict = {'categories': top_category_list, 
                    'pages': page_list, 
                    'cat_list': cat_list}

    context = RequestContext(request, context_dict)
    return render_to_response('rango/index.html', context=context) 

如果 django >= 1.3,您可以更改最后两行

    return render(request, 'rango/index.html', context_dict)

关于你的其他问题

context_dict = {'categories': top_category_list}创建新字典

context_dict['categories'] = top_category_list 将新条目分配(或添加)到现有字典

【讨论】:

  • 感谢您的解释!虽然我仍然很困惑..为什么我们不能只使用一个上下文?
【解决方案2】:

这几乎完全一样,第一个是定义新字典并将新的键/值放入其中,而第二个只是将新的 key:value 放入其中,因为 dic 已经定义。

在现代 django 中,你可以

return render(request, 'index.html', context_dic) 

render 已经为你处理 RequestContext 了。这种方式可能会避免一些混乱

【讨论】:

  • 在 render_to_response 我们使用 context_dict 和 context 并且只渲染一个上下文?这是正确的吗?
  • @Anita no,在render 中你不需要使用context,你只需要在这个例子中是context_dict 的模板上下文
【解决方案3】:

实际上,在 Django 1.11 中,这个小改动似乎可行。

定义字典:(不需要上下文,只需要字典) 然后换行取dict:

return render_to_response('rango/index.html', context_dict)

这对于模板视图更有意义,您从请求中读取并返回带有(结果)上下文的响应。

注意:当需要从标头(请求)中获取更多信息时,这可能不起作用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 2014-08-10
    相关资源
    最近更新 更多