sexta-feira, 11 de agosto de 2017

Without LoginRequiredMixin redirect, request and settings show an error 'not defined'


If you don't use this LoginRequiredMixin, you're gonna receive

name settings not defined
name request not defined
name redirect not defined



class BookListView(LoginRequiredMixin,generic.ListView):
    #login_url = '/login/'
    #redirect_field_name = 'redirect_to'
       
    model = Book
    paginate_by = 2  
    context_object_name = 'book_list'   # your own name for the list as a template variable
    #queryset = Book.objects.filter(title__icontains='war')[:5] # Get 5 books containing the title war
    queryset = Book.objects.all()
    template_name = 'books/book_list.html'  # Specify your own template name/location
   
    def get_context_data(self, **kwargs):
        if not request.user.is_authenticated:
            return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
        else:
            # Call the base implementation first to get a context
            context = super(BookListView, self).get_context_data(**kwargs)
            # Get the blog from id and add it to the context
            context['some_data'] = 'This is just some data'
            return context

Nenhum comentário:

Postar um comentário