[Answered ]-How to send user IP address from ListView to template?

1👍

get_context_data is not called with the request, it is an attribute of the view. You thus can access the request with self.request:

class PostListView(ListView):
    model = Post
    template_name = 'blog/home.html' 
    context_object_name = 'posts'
    ordering = ['-date_posted']
    paginate_by = 5
    
    #                   no request ↓
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['ip'] = self.request.session.get('ip', 0)
        return context

Leave a comment