Anita
Anita

Reputation: 3156

What is the difference between context_dict and context in django, python?

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!

Upvotes: 1

Views: 2675

Answers (3)

Gunnar Sigfusson
Gunnar Sigfusson

Reputation: 11

Actually in Django 1.11 this small change seems to work.

Define the dict: (context not needed, only the dict) Then change the line to take the dict:

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

Which makes more sense for a template view, you read from the request and return the response with the (result) context.

Note: This may not work in cases when additional information is needed from header (request)

Upvotes: 0

sax
sax

Reputation: 3806

  • context_dict is a simple dictionary

  • context 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

Upvotes: 3

doniyor
doniyor

Reputation: 37894

This is almost exactly the same, the first one is defining new dictionary and putting new key/values in it, whereas the second one is just putting new key:value in it because the dic has been already defined.

In modern django, you do

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

Where render already handles RequestContext for you. This way may save some confuses

Upvotes: 0

Related Questions