3

I have defined a custom user model, which is working fine and all that. However, in a certain view I would like the user to list all registered users, but I can't seem to get that to work. I was hoping to be able to access all users from the template that is used to render the view, but I don't know where to start.

This is what I've got so far - which works when using the original User model, but it doesn't work with my custom one.

views.py

class UsersView(TemplateView):
    template_name = 'customer/users/users.html'

users.html

<h1>Users</h1>

<ul>
  {% for user in object_list %}
    <li class="user">{{ user }}</li>
  {% endfor %}
</ul>

urls.py

url(r'^customer/users/', views.UsersView.as_view(), name='users'),

models.py

class CustomUser(AbstractBaseUser):
    """
    Abstraction of a user.

    first_name - The first name of the user
    last_name - The last name of the user
    email - The email and username of the user
    project - The project that the user is part of
    is_active- Determines if the user is active, i.e. is the user "alive"
    is_project_admin - Determines if the user has access to the project admin views
    is_superuser - Determines if the user has full access to the entire database. Usually not.

    This model is used to store information about users.
    """

    first_name = models.CharField(max_length=200, blank=True, help_text="The first name of the user.")
    last_name = models.CharField(max_length=200, blank=True, help_text="The last name of the user.")
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
        help_text="The email and username of the user. Required."
    )

    project = models.ForeignKey(Project, null=True, blank=True, help_text="The project that this user is part of.", related_name='users')

    is_active = models.BooleanField(
        default=True, 
        help_text="Determines whether the user is active or not. ",
        verbose_name="active"
    )
    is_project_admin = models.BooleanField(
        default=False, 
        help_text="Determines if the user has admin access in a project.",
        verbose_name="project admin"
    )
    is_superuser = models.BooleanField(
        default=False, 
        help_text="CAUTION - enabling this gives the user full admin access and access to the entire database. Only for ArcCore admins.",
        verbose_name="superuser"
    )

    objects = MyUserManager()

    USERNAME_FIELD = 'email'

    class Meta:
        verbose_name = 'user'
        verbose_name_plural = 'users'

    def get_full_name(self):
        #If the full name is not specified, return email
        if self.first_name == "" and self.last_name == "":
            return self.email
        else:
            return self.first_name + " " + self.last_name
    get_full_name.short_description = 'Name'

    def get_short_name(self):
        return self.first_name

    def __str__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        return True

    @property
    def is_staff(self):
        "Is the user a superuser?"
        return self.is_superuser

Please disregard any bad code in models.py, since I am new to both Python and Django and I haven't started refactoring yet.

Does anyone know how to list all my users in the tempate?

Thanks in advance!

axelniklasson
  • 55
  • 1
  • 8

2 Answers2

2

try with the below view function

class UsersView(TemplateView):
    template_name = 'customer/users/users.html'

    def get_context_data(self,**kwargs):
        context = super(UsersView,self).get_context_data(**kwargs)
        context['object_list'] = CustomUser.objects.all()
        return context
Venkatesh Bachu
  • 2,348
  • 1
  • 18
  • 28
  • Perhaps worth putting a [link to the docs](https://docs.djangoproject.com/en/1.9/ref/class-based-views/generic-display/) – ryanjdillon Feb 14 '16 at 06:09
0

I don't understand how this code could ever have worked. Your view is a simple template view, and never does anything to get a list of users: that applies as much to the default User model as to your custom one.

You need to subclass ListView instead of TemplateView, and set the model attribute to your User class.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895