Python 获取模板中所有自定义用户的列表(Django)
我已经定义了一个自定义用户模型,它运行良好。然而,在某种视图中,我希望用户列出所有已注册的用户,但我似乎无法实现这一点。我希望能够从用于渲染视图的模板访问所有用户,但我不知道从哪里开始 这就是我到目前为止所得到的——在使用原始用户模型时可以使用,但在我的自定义模型中不起作用 views.py users.html models.py 请忽略models.py中的任何错误代码,因为我对Python和Django都是新手,而且还没有开始重构 有人知道如何在tempate中列出我的所有用户吗Python 获取模板中所有自定义用户的列表(Django),python,django,templates,django-custom-user,Python,Django,Templates,Django Custom User,我已经定义了一个自定义用户模型,它运行良好。然而,在某种视图中,我希望用户列出所有已注册的用户,但我似乎无法实现这一点。我希望能够从用于渲染视图的模板访问所有用户,但我不知道从哪里开始 这就是我到目前为止所得到的——在使用原始用户模型时可以使用,但在我的自定义模型中不起作用 views.py users.html models.py 请忽略models.py中的任何错误代码,因为我对Python和Django都是新手,而且还没有开始重构 有人知道如何在tempate中列出我的所有用户吗 提前谢谢
提前谢谢 我不明白这段代码是怎么工作的。您的视图是一个简单的模板视图,从不做任何事情来获取用户列表:这适用于默认用户模型和自定义用户模型
您需要子类化而不是TemplateView,并将
model
属性设置为您的用户类。尝试使用下面的视图功能
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
您是否尝试在模板中使用{{user.email}}而不是{{user}}?@VenkateshBachu是的,问题在于访问所有用户-例如在循环外使用{{user}时,我得到了我想要的电子邮件。我似乎无法以正确的方式访问所有用户。你能用view更新你的问题吗function@VenkateshBachu添加了视图类。您是否在url中添加了CustomUser模型名称?我考虑过,但我希望模板能够做很多事情,TemplateView似乎是正确的。也许值得一提
<h1>Users</h1>
<ul>
{% for user in object_list %}
<li class="user">{{ user }}</li>
{% endfor %}
</ul>
url(r'^customer/users/', views.UsersView.as_view(), name='users'),
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
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