Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 491437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T02:00:40+00:00 2026-05-13T02:00:40+00:00

The django books gives the local trick in order to avoid to type a

  • 0

The django books gives the local trick in order to avoid to type a long list of parameters as context dictionnary

http://www.djangobook.com/en/2.0/chapter04/

Example:

def current_datetime(request):
    dt_now = datetime.datetime.now()
    return render_to_response('current.html', {'dt_now': dt_now})

becomes:

def current_datetime(request):
    dt_now = datetime.datetime.now()
    return render_to_response('current.html', locals())

It recommends this to lazy programmers but points out some overhead which may have an impact on performance.

I would like to know if some of you are using the locals trick on real applications. Do you recommend it or is it a bad practice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T02:00:40+00:00Added an answer on May 13, 2026 at 2:00 am

    I don’t like repetition — I think “DRY”, “Don’t Repeat Yourself”, is a key programming principle. As a consequence, I have indeed used locals() in similar situations. Django template rendering is far from the only situation of this kind: the general case is “a function or operator which accepts a dict, but doesn’t mind if the dict contains extra entries”. (For example, ordinary string formatting in Python is another such case).

    However, there’s a countervailing principle: programs should be understandable in as localized a way as feasible — that helps maintenance and refactoring (as it obviates the need to study other files to check what refactorings are acceptable). This suggests, for the locals() case, that it’s OK if the template (or string format, etc) is a local literal (a rare case where only few variables are probably being used and thus locals() is not a huge win!-), but problematic in the normal case where the template lives in a different file.

    So, using locals(), in most cases, seriously hampers refactoring. In almost every situation in Python, local variables and their names can freely be changed as part of a local refactoring, since they have no “externally visible” effect… but using locals() breaks that — suddenly you can’t safely rename a variable to a different name offering better clarity, refactor code flow in a manner that removes the need for a variable, etc, etc, without each and every time studying a separate template file to check if the old name might not be needed (and possibly editing the template file, which can be non-trivial, for example if it’s maintained in several different natural languages for i18n/L10n purposes).

    As a consequence, in addition to the secondary issue of performance, there is strong pressure against using locals() in “serious”, “production” code — code that does need long term maintenance and therefore easy refactoring and locality. So, when I’m “programming as best I know how”, rather than “cutting corners”, I’m aware I had better avoid locals().

    The values that you want to have in the context in which the template is rendered are not necessarily “naturally” available as local bare-names, after all; maybe some or many of them are results of computations, items from lists or dictionaries, and the like. In this case, the temptation to “cut corners” with locals() is easier to avoid if you just accumulate those values into a suitable dictionary rather than assigning them local bare-names.

    It’s not the easiest tradeoff, because two good principles (avoiding repetition, and having good locality) are inevitably clashing — therefore, good question! And not one entirely susceptible to sharp black or white answers, which is why I’ve tried to expand on both sides. In the end, I think it’s one of those “style” aspects where a programming team might be well advised to adopt a team-uniform style guideline and stick to it — at least it removes the need to make a decision over and over every time the issue arises, and produces a more homogeneous (and thereby maintainable) code base. [[I have to confess that this specific point has never been explicitly addressed in the style guidelines of teams I’ve been in, though, although many others have!-)]]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm learning Django (1, 3, 1, 'final', 0) through this resource: http://www.djangobook.com/en/2.0/chapter05/ I
In Django I am using two applications: python manage.py startapp books python manage.py startapp
I have a Django view that returns a list of dicts like so data
I want to do do a case-insensitive startswith query in Django: books = Book.objects.filter(title__iexact__startswith=query)
I have a Django database of books, with attached transactions. In the admin interface,
I am reading 2 quite popular django books, both recommend using django's admin user
Am working with django Publisher example, I want to list all the publishers in
I've created a basic Django app that contains books/authors/publishers as per the Django Book
The Django documentation gives examples for using annotate() to produce aggregate results based on
http://pastebin.com/Aa5rJxv8 i have django problem above, i tried to explain i need to show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.