Django Tastypie [英] Django Tastypie

查看:148
本文介绍了Django Tastypie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个需要使用身份验证的移动应用程序。如何实现以下目标:

I am creating a mobile app where I need to use authentication. How can I achieve the following:


  1. 我需要创建一个用户。在创建用户之后,需要发送 Api_client 和秘密作为对用户的响应。

  2. 我有一个功能来执行验证。创建用户后,需要调用移动验证功能。

  3. 重要的是,如何阻止使用for循环并开始添加用户的用户?

  1. I need to create a user. After creating the user it needs to send Api_client and a secret as a response to the user.
  2. I have a function to perform verification. After creating the user it needs to call the function for mobile verification.
  3. Importantly, how can I stop a user who uses a for loop and starts adding users?

我尝试过:

models.signals.post_save.connect(create_api_key, sender=User)

创建了一个API密钥,但不将其作为响应发送

That created an API key but is not sending it as a response when creating the user is successful.

推荐答案

这是我从你的问题中了解到的:


  1. 您希望移动应用的任何用户以匿名身份登录,作为Django应用程序的用户。

  2. 此请求必须触发创建Tastypie api_key,然后返回。

  3. 您希望防止此请求被垃圾邮件发送。

我不明白这一点:

我有一个移动功能没有验证e用户需要调用该功能进行移动验证。

"I have a function for mobile without verification. After creating the user it needs to call the function for mobile verification."

回答我得到的几点:


  1. 看到这个关于使用Tastypie进行用户注册的这个问题 如何使用django-tastypie API以编程方式创建或注册用户?,特别是这部分: / p>

  1. See this SO question regarding user registration with Tastypie How to create or register User using django-tastypie API programmatically?, notably this part :

def obj_create(self, bundle, request=None, **kwargs):
    username, password = bundle.data['username'], bundle.data['password']
    try:
        bundle.obj = User.objects.create_user(username, '', password)
    except IntegrityError:
        raise BadRequest('That username already exists')
    return bundle

有关完整的演练,请查看此文章 http://psjinx.com/programming/2013/06/07/so-you-want-to-create-users - 使用djangotastypie /

你正在关于api_key创建的正确轨道,除了你必须告诉api实际上发回它。您可以使用常规方式(它需要另一个请求):

You're on the right track regarding the api_key creation, except you have to tell the api to actually send it back. You can use the regular way (it requires another request, though) :

即可以从UserResource访问,如上面链接的文章所述,具体来说:


i.e make it accessible from UserResource, as described in the article linked above, specifically :

def dehydrate(self, bundle):
    bundle.data['key'] = bundle.obj.api_key.key

    try:
        # Don't return `raw_password` in response.
        del bundle.data["raw_password"]
    except KeyError:
        pass

    return bundle

如果您想在用户注册后立即发送,请勿忘记将always_return_data设置为True,并将将api_key添加到响应

If you want to send it right after a User's registration, don't forget to set "always_return_data" to True and add the api_key to the response.

垃圾邮件/循环注册:

您应该查看您的服务器的功能关于这个事情。例如,假设您使用的是Nginx: http://wiki.nginx.org/NginxHttpLimitReqModule

You should look into your server's capabilities regarding this matter. For example, assuming you're using Nginx : http://wiki.nginx.org/NginxHttpLimitReqModule

另一个选择可能是使用这个: http://django-ratelimit-backend.readthedocs.org/en/latest/

Another option might be to use this : http://django-ratelimit-backend.readthedocs.org/en/latest/

希望这有帮助!

请问,

这篇关于Django Tastypie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆