DRF令牌认证教程 [英] DRF Token Authentication Tutorial
问题描述
另外,出于生产目的使用第三方软件更实用吗?或者设置自己的(这是一个个人项目,所以时间贡献不是一个问题)。
我的令牌代码的代码: Github
在settings.py
INSTALLED_APPS =(
...
'rest_framework.authtoken'
)
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES':[
'rest_framework.authentication.TokenAuthentication',
],
} b $ b
在signals.py
从django.conf导入设置
从django.db.models.signals导入post_save
从django.dispatch导入收件人
从rest_framework.authtoken.models导入令牌
@receiver(post_save,sender = settings.AUTH_USER_MODEL)
def create_auth_token(sender,instance = None,created = False,** kwargs):
如果创建:
Token.objects.create(user = instance)
在views.py
class ExampleAuthToken(APIView):
def post(self,request,format = None):
username = request.data.get(username)
password = request.data.get(password)
try:
user = User.objects.get(username = username)
除了User.DoesNotExist:
user = User.objects.create_user(username = username)
user.set_password(password)
user.save()
content = {
'user':unicode(user.username),
'token':unicode(user.auth_token),
}
返回响应(内容)
在urls.py
urlpatterns = [
url(r'^ authtoken /',ExampleAuthToken.as_view(),name ='authtoken'),
]
使用ang调用ular j s;> $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
};
$ .post(http:// localhost:8000 / authtoken /,凭证{
success:function(data){
console.log(data);
}
}
I am pretty new to Django, but I want to learn how to implement a DRF Token Authentication with Angularjs. Some of the tutorials I have found haven't been too helpful in showing how to set it up, along with their source code etc...
Also, for production purposes, is it more practical to use a third party package? Or set up my own (it's for a personal project, so time contribution is not an issue).
My Buggy Code for Token Auth: Github
In settings.py
INSTALLED_APPS = (
...
'rest_framework.authtoken'
)
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
],
}
In signals.py
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from rest_framework.authtoken.models import Token
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
if created:
Token.objects.create(user=instance)
In views.py
class ExampleAuthToken(APIView):
def post(self, request, format=None):
username = request.data.get("username")
password = request.data.get("password")
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
user = User.objects.create_user(username=username)
user.set_password(password)
user.save()
content = {
'user': unicode(user.username),
'token': unicode(user.auth_token),
}
return Response(content)
In urls.py
urlpatterns = [
url(r'^authtoken/', ExampleAuthToken.as_view(), name='authtoken'),
]
To call using the angularjs;
var credentials = {
username: "root",
password: "root123"
};
$.post("http://localhost:8000/authtoken/", credentials {
success: function(data){
console.log(data);
}
}
这篇关于DRF令牌认证教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!