anuska117
anuska117

Reputation: 3

Django - display list of all users from auth.models in ListView Django

Hi i am working on a Hotel website and am trying to display list of all the users from the admin site onto my dashboard in a listview

class UsersListView(ListView):
template_name = 'dashboard/users/userlist.html'
login_url = '/login/'
redirect_field_name = 'user_list'


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

The above code only displayed the logged in user not all the list. Is there a way to display the active and inactive state of the users?

<thead>
                <tr>
                  <th>SN</th>
                  <th>Users</th>
                  <th>Email</th>
                  <th>Status</th>
                </tr>
              </thead>
              <tbody>
                {% for user in object_list %}
                <tr>
                  <td>{{forloop.counter}}</td>
                  <td>{{user.username }}</td>
                  <td>{{user.email}}</td>
                  <td>{{user.is_active}}</td>

Upvotes: 0

Views: 1445

Answers (1)

Hagyn
Hagyn

Reputation: 962

Here you are filtering the User QuerySet to only show you the user that is doing the request to the server.

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

Getting all users is as simple as using the .all() function:

def get_queryset(self):
    return User.objects.all()

You should check the Django QuerySet API reference to understand how to work with QuerySet objects.

Upvotes: 1

Related Questions