小编lef*_*ffe的帖子

Django检查并设置用户组的权限

我有一个名为baseusers用户组,其中包含我通过管理员添加的一组权限.如果baseusers不存在,我想创建它并设置它现在拥有的相同权限.我可以创建组但​​是如何从shell中找到权限然后将其添加到我的代码中?例如,两个权限名为can_fm_listcan_fm_add.应用程序称为fileman.但我需要使用id:s吗?这是我想要设置权限的代码.

userprofile/views.py

from fileman.models import Setting (Has the permissions)
from fileman.models import Newuserpath
from django.contrib.auth.models import Group

def register(request):

...

            if Group.objects.count() > 0:
            newuser.groups.add(1)
            else:
            newgroup = Group(name='baseusers')
            newgroup.save()
            newuser.groups.add(1)
Run Code Online (Sandbox Code Playgroud)

fileman/models.py

class Setting(models.Model):
    owner = AutoForeignKey(User, unique=True, related_name='fileman_Setting')
    root = models.CharField(max_length=250, null=True)
    home = models.CharField(max_length=250, null=True)
    buffer = models.TextField(blank=True)
    class Meta:
        permissions = (
            ("can_fm_list", _("Can look files list")),
            ("can_fm_add", _("Can upload files")),
            ("can_fm_rename", _("Can rename …
Run Code Online (Sandbox Code Playgroud)

django django-models django-views

12
推荐指数
2
解决办法
2万
查看次数

包含 Django templatetag 用户对象

嘿,我现在一直在为这个问题苦苦挣扎。我试图将我的用户对象传递给模板,以便我可以列出它们或列出用户名。感谢到目前为止我从这里得到的帮助,我得到了这个。

from django.template import Library, Node, Template, VariableDoesNotExist,      TemplateSyntaxError, \
                        Variable
from django.utils.translation import ugettext as _
from django.contrib.auth.models import User
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import models

register = Library()

class GetAllUsers(Node):
    def __init__(self, varname):
        # Save the variable that we will assigning the users to
        self.varname = varname
    def render(self, context):
        # Save all the user objects to the variable and return the context to the  template
        context[self.varname] = User.objects.all()
        return ''

@register.tag(name="get_all_users") …
Run Code Online (Sandbox Code Playgroud)

django django-templates

5
推荐指数
1
解决办法
2094
查看次数