2

I want to display all the users, in my template but I cannot. I think there is a mistake in my codes but I cannot find it. Here is codes.

views.py

class UsersView(TemplateView): template_name = 'users.html' def get_context_data(self, **kwargs): context = super(UsersView, self).get_context_data(**kwargs) context['object_list'] = UserList.objects.all() return context 

models.py

class UserList(AbstractBaseUser): 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." ) 

users.html

 {% extends 'blog/base.html' %} {% block content %} <h1>Users:</h1> <ul> {% for users in object_list %} <li class="user">{{ user }}</li> {% endfor %} </ul> {% endblock %} 

And this is the page that I get:

Where is my mistake? Thank you.

4
  • 1
    What exacty is the page you get? Commented Sep 13, 2018 at 13:27
  • I find it rather strange that you name the model UserList. Normally Django models are named in a singular way, here you "hint" that an object is in fact a list of Users. Commented Sep 13, 2018 at 13:28
  • 1
    If you want the users registered in the Django system, it is probably the django.contrib.auth.models.User model you are after, the default model used for authentication. Commented Sep 13, 2018 at 13:29
  • There is my page: prnt.sc/ktz1cf Commented Sep 13, 2018 at 13:30

2 Answers 2

2

Your code includes the following:

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

The trouble with this is that the user template variable is not defined. Change users to user in the loop header and you should be OK.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use django.contrib.auth.models.User on your view with request.user.get_username() with this you use a standard user with Django. If go go admin URL you see user default.

For call in template :

{{ user.get_username }}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.