簡體   English   中英

獲取模板中所有自定義用戶的列表(Django)

[英]Get list of all custom users in template (Django)

我已經定義了一個自定義用戶模型,它正常工作。 但是,在某個視圖中我希望用戶列出所有注冊用戶,但我似乎無法讓它工作。 我希望能夠從用於呈現視圖的模板訪問所有用戶,但我不知道從哪里開始。

這是我到目前為止所使用的 - 在使用原始用戶模型時有效,但它不適用於我的自定義模型。

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

請忽略models.py中的任何錯誤代碼,因為我是Python和Django的新手,我還沒有開始重構。

有誰知道如何在tempate中列出我的所有用戶?

提前致謝!

嘗試使用以下視圖功能

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

我不明白這段代碼是如何工作的。 您的視圖是一個簡單的模板視圖,從不做任何事情來獲取用戶列表:這對於默認用戶模型和自定義用戶模型同樣適用。

您需要繼承ListView而不是TemplateView,並將model屬性設置為User類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM