简体   繁体   English

Django Rest框架中的用户注册

[英]user registration in django rest framework

I am creating a server side for android application in DRF which will require user registration and login\\logout endpoints. 我正在DRF中为android应用程序创建服务器端,这将需要用户注册和login \\ logout端点。 also obviously different permissions when a user logged in. 用户登录时的权限也明显不同。

I followed the rest framework tutorial here - http://www.django-rest-framework.org/tutorial/1-serialization/ and this example really seeme to cover it all beside the user registration (creation). 我在这里遵循了其余框架教程-http: //www.django-rest-framework.org/tutorial/1-serialization/ ,这个示例确实似乎涵盖了用户注册(创建)之外的所有内容。

In the tutorial they do it from the command line (and they create a superuser). 在本教程中,他们从命令行进行操作(并创建了超级用户)。 I think the tutorial example is really good for my needs besides not having the registration endpoint. 我认为本教程示例除了没有注册端点之外,还非常符合我的需求。

My question are: 我的问题是:

If it matters, my user model in actually a UserProfile model which includes the user model and added a phone_number... 如果重要的话,我的用户模型实际上是一个UserProfile模型,其中包括用户模型并添加了phone_number ...

Thanks a lot! 非常感谢!

A regular user has different authorities from superuser and you should customize view for a specific user. 普通用户具有与超级用户不同的权限,因此您应该为特定用户自定义视图。 Here is link for you to create user in django. 这是您在django中创建用户的链接

Hope it helps. 希望能帮助到你。

I've copied it from Django documentation as an answer for your first question. 我已经从Django文档中复制了它,作为第一个问题的答案。

One of the most powerful parts of Django is the automatic admin interface. Django最强大的部分之一是自动管理界面。 Best thing is that you can customise it easily. 最好的事情是您可以轻松自定义它。

If logged in as a superuser, you have access to create, edit, and delete any object (models). 如果以超级用户身份登录,则您有权创建,编辑和删除任何对象(模型)。

You can create staff user using staff flag. 您可以使用人员标记创建人员用户。 The “staff” flag controls whether the user is allowed to log in to the admin interface (ie, whether that user is considered a “staff member” in your organization). “工作人员”标志控制是否允许用户登录到管理界面(即,该用户是否在您的组织中被视为“工作人员”)。 Since this same user system can be used to control access to public (ie, non-admin) sites, this flag differentiates between public users and administrators. 由于可以使用同一用户系统来控制对公共(即非管理员)站点的访问,因此此标志区分公共用户和管理员。

“Normal” admin users – that is, active, non-superuser staff members – are granted admin access through assigned permissions. 通过分配的权限为“普通”管理员用户(即活跃的非超级用户职员)授予管理员访问权限。 Each object editable through the admin interface has three permissions: a create permission, an edit permission and a delete permission for all the models you had created. 通过管理界面可编辑的每个对象都具有三个许可权:您创建的所有模型的创建许可权,编辑许可权和删除许可权。

Django's admin site uses a permissions system that you can use to give specific users access only to the portions of the interface that they need. Django的管理站点使用权限系统,您可以使用该权限系统使特定用户仅访问他们需要的界面部分。 When you create a user, that user has no permissions, and it's up to you to give the user specific permission 创建用户时,该用户没有权限,由您来授予用户特定的权限

You can do something like this for the second question you've asked. 您可以对第二个问题做类似的事情。

 from django.contrib.auth.models import User
 from rest_framework.views import APIView
 from rest_framework import status

 class Register(APIView):
    def post(self, request):
      user = User.objects.create(
                username=request.data.get('email'),
                email=request.data.get('email'),
                first_name=request.data.get('firstName'),
                last_name=request.data.get('lastName')
            )
     user.set_password(str(request.data.get('password')))
     user.save()
     return Response({"status":"success","response":"User Successfully Created"}, status=status.HTTP_201_CREATED)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM