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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:08:22+00:00 2026-05-30T20:08:22+00:00

I am trying to understand the code for the mixins posted at this blog

  • 0

I am trying to understand the code for the mixins posted at this blog post.

These mixins call the login_required decorator from django.contrib.auth.decorators within the mixins , but they do so decorated by the method_decorator from django.utils.decorators.
In the sample code below I dont understand why I need to decorate the login_required decorator .

from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
class LoginRequiredMixin(object):
    """
    View mixin which verifies that the user has authenticated.

    NOTE:
        This should be the left-most mixin of a view.
    """
    # Why do I need to decorate login_required here
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(LoginRequiredMixin, self).dispatch(*args, **kwargs) 

The method_decorator decorator says it is used to “Converts a function decorator into a method decorator” But in test code I can use my decorator even without the method_decorator.

My decorator

def run_eight_times(myfunc):
    def inner_func(*args, **kwargs):
        for i in range(8):
            myfunc(*args, **kwargs)
    return inner_func 

My class which calls the above decorator directly produces the same result as if I called the decorator decorated by method_decorator

from django.utils.decorators import method_decorator
class Myclass(object):

    def __init__(self,name,favorite_dish):
        self.name = name
        self.favorite_dish = favorite_dish

    # This next line is not required
    #@method_decorator(run_eight_times)
    @run_eight_times
    def undecorated_function(self):
        print "%s likes spam in his favorite dish %s" % (self.name,self.favorite_dish) 
  • 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-30T20:08:23+00:00Added an answer on May 30, 2026 at 8:08 pm

    Django’s method_decorator is set up to pass the self argument in correctly to the decorated function. The reason this doesn’t show up in the test cases you wrote above with the run_eight_times decorator is that the inner_func in run_eight_times blindly passes all arguments in to myfunc via *args and **kwargs. In general, this won’t be the case.

    To see this with your example, try the following:

    from django.utils.decorators import method_decorator
    
    def run_eight_times(myfunc):
        def inner_func(what_he_likes, **kwargs):
            # override...
            what_he_likes = 'pizza'
            for i in range(8):
                myfunc(what_he_likes, **kwargs)
        return inner_func
    
    class MyClass(object):
    
        def __init__(self, name, favorite_dish):
            self.name = name
            self.favorite_dish = favorite_dish
    
        # This next line required!
        @method_decorator(run_eight_times)
        #@run_eight_times
        def undecorated_function(self, what_he_likes):
            print "%s likes %s in his favorite dish %s" % (
                self.name, what_he_likes, self.favorite_dish
            )
    
    def main():
        inst = MyClass('bob', 'burrito')
        inst.undecorated_function('hammy spam')
    
    if __name__ == '__main__':
        main()
    

    Specifically, Django’s view decorators will return a function with a signature (request, *args, **kwargs). For a class based-view, this should be (self, request, *args, **kwargs). That’s what method_decorator does – transform the first signature into the second.

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

Sidebar

Related Questions

I'm trying to understand this code from Deitels 5th edition c book. If I'm
I’m trying to understand code from http://www.yesodweb.com/book/conduits . After some fixes (like replacing Resource
Trying to understand the math of this code snippet. A token is provided which
I am trying to understand this code: #include<stdio.h> int main() { extern int a;
I'm really confused here. I am trying to understand this code (it's javascript) but
I'm trying to understand this code, and I still don't know why it starts
I am trying to understand this code snippet: while row = input.gets row.strip! next
I'm trying to understand why my code isn't working. this is a piece of
I trying to understand this piece of code Function.prototype.method = function (name, fn) {
I'm trying to understand this code and I can't :( $time = date('Y-m-d', strtotime('-30

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.