# about login\_required

**URL:** <https://forum.djangoproject.com/t/about-login-required/15231>\
**Category:** Forms & APIs\
**Created:** [August 4, 2022, 6:28am UTC](https://forum.djangoproject.com/t/about-login-required/15231 "2022-08-04T06:28:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkt1112](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tkt1112/32/9177_2.png) [@tkt1112](https://forum.djangoproject.com/u/tkt1112)\
**Post date:** [August 4, 2022, 6:28am UTC](https://forum.djangoproject.com/t/about-login-required/15231/1 "2022-08-04T06:28:28Z")

</div>

Where should I write “@login\_required” in “class PostCreate(generic.CreateView):”

I got an error’PostCreate’ object has no attribute ‘user’ when I fill in above the “def get\_context\_data(self, \*\*kwargs):”

Other pages display the login page normally.

```auto
Views.py

from django.shortcuts import render, redirect
from datetime import datetime
from .forms import InquiryModelForm, PostModelForm, PostCreateForm
from .models import Repair_Case, Post, ParentCategory
from .filters import InquiryFilter, PostFilter
from django.contrib.auth.decorators import login_required
from django.views import generic

@login_required
def inquiry_Parts_Summary(request):
    form = Post.objects.all().order_by('-Data')
    myFilter = PostFilter(request.GET, queryset=form)
    form = myFilter.qs
    
    context = {
        'form': form, 'myFilter':myFilter,
    }
    
    return render(request, 'App/inquiry_Parts_Summary.html', context)

@login_required
def inquiry_detail(request, pk):
    inquiry = Repair_Case.objects.get(id=pk)
    
    context = {
        'inquiry': inquiry, 
    }
    
    return render(request, 'AZApp/inquiry_detail.html', context)

class PostCreate(generic.CreateView):
    model = Post
    form_class = PostCreateForm
    success_url = 'inquiry-Parts_Summary'  

   @login_required
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['parentcategory_list'] = ParentCategory.objects.all()
        return context

```

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [August 4, 2022, 10:36am UTC](https://forum.djangoproject.com/t/about-login-required/15231/2 "2022-08-04T10:36:39Z")

</div>

Hey there!  
In class-based-views you decorate the view on the url, [check out the docs](https://docs.djangoproject.com/en/4.1/topics/class-based-views/intro/#decorating-in-urlconf).

Edit:  
If you want to decorate the method, there’s a decorator for the login\_required decorator 😄. [Also from the docs](https://docs.djangoproject.com/en/4.1/topics/class-based-views/intro/#decorating-the-class).

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 4, 2022, 10:42am UTC](https://forum.djangoproject.com/t/about-login-required/15231/3 "2022-08-04T10:42:20Z")

</div>

The above answer is good if you have a generic decorator you wish to apply. However, for the specific purpose of checking for a user being logged in, it’s probably better to use the `LoginRequiredMixin`. See [Using the Django authentication system | Django documentation | Django](https://docs.djangoproject.com/en/4.1/topics/auth/default/#the-loginrequired-mixin)
