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 7866169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:18:44+00:00 2026-06-03T00:18:44+00:00

I constantly see myself having to add the same extra variable to the context

  • 0

I constantly see myself having to add the same extra variable to the context of many of my views.

def get_context_data(self, **kwargs):
    # Call the base implementation first to get a context
    context = super(MyListView, self).get_context_data(**kwargs)
    # Add in the house
    context['house'] = self.get_object().house
    return context

As I don’t like repeating myself, I thought I could create a new class extending the view, and then I could base all my views on the new extended view class. The thing is, there are 4 classes of views I use: CreateView, UpdateView, ListView, and DeleteView. Do I really have to create a new class for each one of them?

Isn’t there something like a Django "base" view class? Maybe a smarter way to do this?

  • 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-06-03T00:18:45+00:00Added an answer on June 3, 2026 at 12:18 am

    Create a Mixin:

    from django.views.generic.base import ContextMixin
    
    class HouseMixin(ContextMixin):
      def get_house(self):
        # Get the house somehow
        return house
    
      def get_context_data(self, **kwargs):
        ctx = super(HouseMixin, self).get_context_data(**kwargs)
        ctx['house'] = self.get_house()
        return ctx
    

    Then in your other classes you’d use multiple inheritance:

    class HouseEditView(HouseMixin, UpdateView):
      pass
    
    class HouseListView(HouseMixin, ListView):
      pass
    

    and so on, then all these views will have house in the context.

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

Sidebar

Related Questions

In the examples, I constantly see **kwargs passed around with no mention of where
Possible Duplicate: What is the cost of '$(this)'? I constantly see in some developers
I would like to constantly (every 30 seconds) check and see if secondary(sdb) hard
I constantly find myself writing similar code like the example below: if (object[Object Name]
The variable 'totalBytes' is constantly at -1 so I cannot calculate/update the progress bar
In my latest quest to learn some assembly language I'm finding myself constantly going
Please see at the end, as I constantly update with latest investigation data. Currently,
I'm currently using a compiler that constantly returns warnings, I don't want to see
This is the first time I see this code myself where we are getting
In .net framework I constantly see overloaded functions like the following, public void Log(string

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.