标签: django-authentication

django-object-permissions vs django-guardian vs django-authority

我已经为Django 1.2+找到了3个行级权限解决方案

有人可以告诉是否有任何推荐比其他人更多,他们的主要区别是什么,等等?

django django-apps django-authentication django-permissions

33
推荐指数
2
解决办法
9885
查看次数

如何在django-rest-framework中使用TokenAuthentication for API

我有一个django项目,使用django-rest-framework来创建api.

想要使用令牌基础认证系统,因此api调用(put,post,delete)只会为授权用户执行.

我安装了'rest_framework.authtoken'并为每个用户创建了令牌.

所以,现在从django.contrib.auth.backends身份验证,它返回用户,auth_token作为属性.(成功时).

现在我的问题是如何将带有post请求的令牌发送到我的api和api端如何验证令牌是否有效并且属于正确的用户?

app rest_framework.authtoken中是否有任何方法可以验证给定用户及其令牌?没发现这个非常有用!

更新(我做的更改):在我的settings.py中添加了这个:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    )
}
Run Code Online (Sandbox Code Playgroud)

还在我的标题中发送令牌,但它仍然无法正常工作:

if new_form.is_valid:
    payload= {"createNewUser":
              { "users": request.POST["newusers"],
                "email": request.POST["newemail"]
                }
              }

    headers =  {'content-type' : 'application/json', 
                'Authorization': 'Token 6b929e47f278068fe6ac8235cda09707a3aa7ba1'}

    r = requests.post('http://localhost:8000/api/v1.0/user_list',
                      data=json.dumps(payload),
                      headers=headers, verify=False)
Run Code Online (Sandbox Code Playgroud)

python django django-authentication django-rest-framework

31
推荐指数
1
解决办法
3万
查看次数

覆盖User对象上的默认get_absolute_url?

我正在尝试为列出django_tables对象创建一个通用表.我已经完成了所有工作,除了get_absolute_urls()我的User对象返回:

/users/<username>/
Run Code Online (Sandbox Code Playgroud)

虽然我可以创建此URL,但它与网站布局的其余部分不匹配,所以我正在寻找另一种方法来执行此操作.有没有办法在不破坏内置身份验证和其他功能的情况下覆盖此值?

django django-urls django-authentication

28
推荐指数
1
解决办法
3579
查看次数

django - 在自动创建用户时设置用户权限

Django 1.5,python 2.6

该模型在特定条件下自动创建用户:

User.objects.get_or_create(username=new_user_name, is_staff=True) 
u = User.objects.get(username=new_user_name)
u.set_password('temporary')
Run Code Online (Sandbox Code Playgroud)

除了设置用户名,密码和is_staff状态之外,我还想设置用户的权限 - 类似于:

u.user_permissions('Can view poll')
Run Code Online (Sandbox Code Playgroud)

要么

u.set_permissions('Can change poll')
Run Code Online (Sandbox Code Playgroud)

这可能吗?谢谢!

python django django-admin django-authentication

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

渲染时的NoReverseMatch:反转''django.contrib.auth.views.login''

我正在使用Django的身份验证,在login.html模板中,以下语句生成错误:

{% url 'django.contrib.auth.views.login' %}
Run Code Online (Sandbox Code Playgroud)

/ login时的TemplateSyntaxError

渲染时捕获NoReverseMatch:反向找到''django.contrib.auth.views.login'',其参数'()'和关键字参数'{}'未找到.

这个网址在我的urls.py中定义:

(r'^login$', 'django.contrib.auth.views.login')
Run Code Online (Sandbox Code Playgroud)

我安装了auth系统:

INSTALLED_APPS = (
    'django.contrib.auth',
...
)
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

authentication django django-authentication django-login

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

设置user_permissions时,Django用户get_all_permissions()为空

我通过管理界面向用户添加了一些权限.

由于某些原因,所有烫发功能都失败了,例如

>>> user.get_all_permissions()
set([])
Run Code Online (Sandbox Code Playgroud)

但直接访问表,工作:

>>> user.user_permissions.all()
(list of permissions as expected)
Run Code Online (Sandbox Code Playgroud)

什么可以导致"get_all_permissions"(和所有的perm函数,如has_perm())失败?

谢谢

django django-admin django-authentication django-permissions

25
推荐指数
1
解决办法
1万
查看次数

如何获得用户权限?

我想检索用户的所有权限作为预设ID的列表,但是:

user.get_all_permissions()
Run Code Online (Sandbox Code Playgroud)

给我许可名称列表.怎么做?

python django django-authentication

25
推荐指数
5
解决办法
3万
查看次数

使用Django中的电子邮件地址或用户名登录用户

我正在尝试创建一个auth后端,以允许我的用户使用他们的电子邮件地址或Django 1.6中的用户名使用自定义用户模型登录.当我使用用户名登录时后端工作,但由于某种原因没有电子邮件.有什么我忘了做的事吗?

from django.conf import settings
from django.contrib.auth.models import User

class EmailOrUsernameModelBackend(object):
    """
    This is a ModelBacked that allows authentication with either a username or an email address.

    """
    def authenticate(self, username=None, password=None):
        if '@' in username:
            kwargs = {'email': username}
        else:
            kwargs = {'username': username}
        try:
            user = User.objects.get(**kwargs)
            if user.check_password(password):
                return user
        except User.DoesNotExist:
            return None

    def get_user(self, username):
        try:
            return User.objects.get(pk=username)
        except User.DoesNotExist:
            return None
Run Code Online (Sandbox Code Playgroud)

编辑:建议我从ModelBackend继承并在我的设置中安装它在我的设置中我有这个AUTHENTICATION_BACKENDS =('users.backends','django.contrib.auth.backends.ModelBackend',)我已经将后端更改为这个:

from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth.backends …
Run Code Online (Sandbox Code Playgroud)

python django django-authentication django-login

25
推荐指数
6
解决办法
1万
查看次数

如何从不同位置检测到多个登录到Django Web应用程序?

我想一次只允许一个经过身份验证的会话,以便在我的Django应用程序中进行单独登录.因此,如果用户登录到给定IP地址的网页,并且使用相同的用户凭据从不同的IP地址登录,我想做某事(登出第一个用户或拒绝访问第二个用户.)

django django-authentication

24
推荐指数
4
解决办法
1万
查看次数

TypeError:login()占用1个位置参数,但给出了2个

我在auth中使用buid写了一个登录视图,django auth.login()给出了上面的错误我的代码,错误代码为500

from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view
from django.contrib.auth.models import User
from django.contrib.auth import authenticate,logout,login


@api_view(['POST'])
def register(request):
    user=User.objects.create_user(username=request.POST['username'],email=request.POST['email'],password=request.POST['password'])
    return Response({'ok':'True'},status=status.HTTP_201_CREATED)

@api_view(['POST'])
def login(request):
    user=authenticate(
        username=request.POST['username'],
        password=request.POST['password']
    )
    if user is not None:
        login(request,user)
        return Response({'ok':'True'},status=status.HTTP_200_OK)
    else:
        return Response({'ok':'False'},status=status.HTTP_401_UNAUTHORIZED)
Run Code Online (Sandbox Code Playgroud)

django django-authentication

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