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

  • Home
  • SEARCH
  • 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 537785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:57:49+00:00 2026-05-13T09:57:49+00:00

I’m trying to refactor a fairly hefty view function in Django. There are too

  • 0

I’m trying to refactor a fairly hefty view function in Django. There are too many variables floating around and it’s a huge function.

Ideally, I want to modularize the view into logical functions.
However, I have to pass the function context around to get easy access to the variables.

For example:

def complex_view(request, slug):
    some complex logic which creates variable abc ...
    ...
    some more complex logic which uses variable abc ...
    ...
    etc.

Should become something like:

def complex_view(request, slug):
    process_X()
    ...somehow pass variables and context to next function...
    process_Y()
    ... 
    etc.

def process_X():
    ...

def process Y():
    ...

I can think of a few ways to do this, some of which were pointed out in this page:
http://mail.python.org/pipermail/tutor/2009-February/067506.html

a. Subfunctions defined in the master view. This seems kludgey since it’s hard to tell which variables are shared and which aren’t.

b. Passing locals() in as a dictionary. This is also kludgey because there are now two separate ways of accessing a variables: xyz and contextDict['xyz']. AND you have to use one in call N, and the next in call N+1 on the stack.

c. Brute force pass in all variables to each function call and return relevant vars. This gets very tedious when there’s a lot of vars involved.

d. In C++/C#, I would simply create a class MyComplexViewContext, define all the shared variables, and create member functions to carry out the work. Then you can use self.xyz for everything inside that class.
I suppose I could use this method in Python as well. Not sure if this is the best way though.

What’s your take on the preferred way to do this in Python/Django?

  • 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-13T09:57:49+00:00Added an answer on May 13, 2026 at 9:57 am

    I like (d) – Create a class for it, and use member functions to do the work.

    In Django, a view is just a ‘callable’ that accepts an HTTPRequest object, and whatever other paramaters your URL routing passes to it.

    Python classes can be callable just like functions, if you define a __call__ method on them, like this:

    class MyView(object):
        def __call__(self, request, slug)
            # do stuff here
    
        def helper_method(self):
            # etc.
    

    Then you can name the class in your urls.py file, and it will be called like any other python function.

    Doing this also lets you turn similar views into object instances:

    class MyView(object):
        def __init__(self, parameters):
            # initialize instance
    
        def __call__(self, request, slug):
            # main view code goes here
    
    first_view = MyView("some parameter")
    second_view = MyView("some other parameter") # creates second object instance
    

    and in urls.py, reference the objects (rather than the class) — the objects can be called like functions as well.

    Other tricks are to use inheritance to define similar views, or to provide some general functionality for a number of similar views in a base class, which specialized view classes then inherit from.

    You can see this slideshow by Simon Willison for more details, or this snippet for a concrete example

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.