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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:53:40+00:00 2026-05-22T17:53:40+00:00

I’m having a bit of trouble understanding how the new CBVs work. My question

  • 0

I’m having a bit of trouble understanding how the new CBVs work. My question is this, I need to require login in all the views, and in some of them, specific permissions. In function-based views I do that with @permission_required() and the login_required attribute in the view, but I don’t know how to do this on the new views. Is there some section in the django docs explaining this? I didn’t found anything. What is wrong in my code?

I tried to use the @method_decorator but it replies “TypeError at /spaces/prueba/ _wrapped_view() takes at least 1 argument (0 given)“

Here is the code (GPL):

from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required, permission_required

class ViewSpaceIndex(DetailView):

    """
    Show the index page of a space. Get various extra contexts to get the
    information for that space.

    The get_object method searches in the user 'spaces' field if the current
    space is allowed, if not, he is redirected to a 'nor allowed' page. 
    """
    context_object_name = 'get_place'
    template_name = 'spaces/space_index.html'

    @method_decorator(login_required)
    def get_object(self):
        space_name = self.kwargs['space_name']

        for i in self.request.user.profile.spaces.all():
            if i.url == space_name:
                return get_object_or_404(Space, url = space_name)

        self.template_name = 'not_allowed.html'
        return get_object_or_404(Space, url = space_name)

    # Get extra context data
    def get_context_data(self, **kwargs):
        context = super(ViewSpaceIndex, self).get_context_data(**kwargs)
        place = get_object_or_404(Space, url=self.kwargs['space_name'])
        context['entities'] = Entity.objects.filter(space=place.id)
        context['documents'] = Document.objects.filter(space=place.id)
        context['proposals'] = Proposal.objects.filter(space=place.id).order_by('-pub_date')
        context['publication'] = Post.objects.filter(post_space=place.id).order_by('-post_pubdate')
        return context
  • 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-22T17:53:40+00:00Added an answer on May 22, 2026 at 5:53 pm

    There are a few strategies listed in the CBV docs:

    Decorate the view when you instantiate it in your urls.py (docs)

    from django.contrib.auth.decorators import login_required
    
    urlpatterns = [
        path('view/',login_required(ViewSpaceIndex.as_view(..)),
        ...
    ]
    

    The decorator is applied on a per-instance basis, so you can add it or remove it in different urls.py routes as needed.

    Decorate your class so every instance of your view is wrapped (docs)

    There’s two ways to do this:

    1. Apply method_decorator to your CBV dispatch method e.g.,

       from django.utils.decorators import method_decorator
       from django.contrib.auth.decorators import login_required
      
       @method_decorator(login_required, name='dispatch')
       class ViewSpaceIndex(TemplateView):
           template_name = 'secret.html'
      

    If you’re using Django < 1.9 (which you shouldn’t, it’s no longer supported) you can’t use method_decorator on the class, so you have to override the dispatch method manually:

        from django.contrib.auth.decorators import login_required
    
        class ViewSpaceIndex(TemplateView):
    
            @method_decorator(login_required)
            def dispatch(self, *args, **kwargs):
                return super(ViewSpaceIndex, self).dispatch(*args, **kwargs)
    
    1. Use a mixin like django.contrib.auth.mixins.LoginRequiredMixin outlined well in the other answers here:

       from django.contrib.auth.mixins import LoginRequiredMixin
      
       class MyView(LoginRequiredMixin, View):
      
           login_url = '/login/'
           redirect_field_name = 'redirect_to'
      

    Make sure you place the mixin class first in the inheritance list (so Python’s Method Resolution Order algorithm picks the Right Thing).

    The reason you’re getting a TypeError is explained in the docs:

    Note:
    method_decorator passes *args and **kwargs as parameters to the decorated method on the class. If your method does not accept a compatible set of parameters it will raise a TypeError exception.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

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.