Mathias Johnson
Mathias Johnson

Reputation: 45

user=request.user - name 'request' is not defined

from django.contrib.auth.models import User
...
class FruitList(ListView):
    model = Fruit
    paginate_by = "2"
    queryset = Fruit.objects.filter(user=request.user)
    context_object_name = "myfruitlist"
    template_name = 'myfruit_list.html'

Error:

name 'request' is not defined

How can I fix it?

Upvotes: 1

Views: 1977

Answers (1)

Łukasz Staniszewski
Łukasz Staniszewski

Reputation: 668

You have to access it this way:

def get_queryset(self):
    return Fruit.objects.filter(user=self.request.user)

You can read about this here in docs

Upvotes: 3

Related Questions