django,python中的context_dict和context之间有什么区别? [英] What is the difference between context_dict and context in django, python?
问题描述
def index(request):
context = RequestContext(request)
top_category_list = Category.objects.order_by(' - likes')[:5]
在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
如果请求.session.get('last_visit'):
#会话具有最后访问的值
last_visit_time = request.session.get('last_visit')
访问= request.session.get('visits',0)
if(datetime.now() - datetime.strptime(last_visit_time [: - 7],%Y-%m-%d%H :%M:%S))。天> 0:
request.session ['visits'] = visits + 1
else:
#get返回None,会话没有上次访问的值。
request.session ['last_visit'] = str(datetime.now())
request.session ['visits'] = 1
#渲染并返回呈现的响应回到用户。
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
是一个简单的字典 -
上下文
是一个实例或RequestContext
里面 render_to_response()
context_dict
是(临时)添加到上下文
实例
代码(在这种情况下)可以写得更清楚(IMHO)为:
def index(request) :
top_category_list = Category.objects.order_by(' - likes')[:5]
在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}
创建新的dict
context_dict ['categories'] = top_category_list
将新条目分配(或添加)到现有字典
I was always using context in my methods until I came across on this view:
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)
In above function there are context_dict and context? Why is that?
Also is there a difference between: context_dict = {'categories': top_category_list} and context_dict['categories'] = top_category_list
or this is exactly the same ?
Thank you guys!
context_dict
is a simple dictionarycontext
is an instance or RequestContext
inside render_to_response()
context_dict
is (temporary) added to the context
instance
the code (in this case) can be written more clearly (IMHO) as:
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)
if django >= 1.3 you can change last two lines with
return render(request, 'rango/index.html', context_dict)
about your other question
context_dict = {'categories': top_category_list}
create e new dict
context_dict['categories'] = top_category_list
assign (or add) a new entry into an existing dictionary
这篇关于django,python中的context_dict和context之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!