我正在尝试在tastypie中编写自定义身份验证.基本上,我想使用post参数进行身份验证,我根本不想使用django auth,所以我的代码看起来像:
class MyAuthentication(Authentication):
def is_authenticated(self, request, **kwargs):
if request.method == 'POST':
token = request.POST['token']
key = request.POST['key']
return is_key_valid(token,key)
Run Code Online (Sandbox Code Playgroud)
这或多或少都是这个想法.问题是我不断收到以下错误:
"error_message": "You cannot access body after reading from request's data stream"
Run Code Online (Sandbox Code Playgroud)
我知道这与我正在访问POST的事实有关,但我无法确定是否有办法解决它.有任何想法吗?谢谢.
编辑:也许我忘了提到最重要的事情.我正在使用github中找到的技巧处理表单数据.我的资源来自多部分资源
class MultipartResource(object):
def deserialize(self, request, data, format=None):
if not format:
format = request.META.get('CONTENT_TYPE', 'application/json')
if format == 'application/x-www-form-urlencoded':
return request.POST
if format.startswith('multipart'):
data = request.POST.copy()
data.update(request.FILES)
return data
return super(MultipartResource, self).deserialize(request, data, format)
Run Code Online (Sandbox Code Playgroud) 我目前正在对我的API执行cURL POST请求
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"username":"theusername", "api_key":"anapikey", "video_title":"a title", "video_description":"the description"}' http://localhost:8000/api/v1/video/
Run Code Online (Sandbox Code Playgroud)
但现在我需要能够将视频文件添加到上传中.我一直在寻找有关使用Tastypie上传文件的几个小时,我还没有想出一个坚实的回复.我需要添加Base64编码吗?如果是这样的话?在我通过POST请求上传文件后如何访问该文件?只是正常的request.FILES动作?我不是要将文件保存到数据库,只是获取文件的路径.
#Models.py
class Video(models.Model):
video_uploader = models.ForeignKey(User)
video_path = models.CharField(max_length=128)
video_views = models.IntegerField(default=0)
upload_date = models.DateTimeField(auto_now_add=True)
video_description = models.CharField(max_length=860)
video_title = models.SlugField()
Run Code Online (Sandbox Code Playgroud)
我对如何为Tastypie实现文件上传系统感到非常困惑,所以任何帮助都将非常感激.谢谢!