django-registration - 特定组的新用户
我正在我的最新产品上使用(和学习)Django,我刚刚安装并开始使用 django-注册应用程序,允许我的用户自动注册。
一切正常,但我正在考虑将每个新用户分配到特定组(已创建),这样我就可以将管理员与普通用户“分开”。
我该怎么做? IMO,关于这个应用程序的文档很糟糕。
我没有创建任何模型或配置来开始使用 django-registration 应用程序。
I'm using (and learning) Django on my newest product, and I just installed and started using django-registration app to allow my users to auto-register themselves.
Everything's working good, but I'm thinking about assigning every new user to a specific group (already created), so I can "separate" the admins from the normal users.
How can I do this? The documentation about this app is awful, IMO.
I didn't create any model or configuration to start using the django-registration app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最终为此做了一个叉子,因为这让我发疯。
https://github.com/kcunning/django-registration
基本上,将以下内容添加到您的设置中.py 文件:
REGISTRATION_DEFAULT_GROUP_NAME="Groupname"
通过注册模块添加的任何用户将在创建时被赋予该组。
I ended up making a fork for this, as this was driving me crazy.
https://github.com/kcunning/django-registration
Basically, add the following to your settings.py file:
REGISTRATION_DEFAULT_GROUP_NAME="Groupname"
Any user added through the registration module will be given that group upon creation.
首先,我不是 dr (django-registration) 的专家,实际上只是检查了它,因为你在问题中提到了它。因此,如果其他人有更好的答案,请提交。无论如何,就这样吧。
我注意到创建用户(或 dr 称之为“配置文件”)的方式可以在位于registration.models.RegistrationManager 中的create_inactive_user 函数中找到。因此,在此函数中:
您可以添加类似以下内容:
您可能可以在没有 get_or_create 的情况下执行此操作,因为我认为您无法使用相同的用户名创建多个用户,但我宁愿安全也不要抱歉。
此方法将导致您编辑 dr 文件(这可能不是最好的做法)并对某些内容(例如默认组名称)进行硬编码,但这应该是一个很好的起点。
附言。默认组名称可以是位于 settings.py 文件中的变量,这将使其更易于访问和美观。示例:
无论如何,希望有所帮助。
To start, I'm not an expert at d-r (django-registration) and actually only checked it out because you mentioned it in your question. So, if anyone else has a better answer please submit it. Anyway, here it goes.
I noticed that the way users (or "profiles" as d-r calls them) are created is found in the create_inactive_user function located in registration.models.RegistrationManager. So, in this function after:
You could add something like:
You could probably do without the get_or_create as I don't think you can create more than one user with the same username, but I would rather be safe than sorry.
This method will cause you to edit the d-r files (which may not be the best thing to do) along with hardcoding certain things (such as the default group name), but it should be a good stepping off point.
PS. the default group name could be a variable located in the settings.py file, which would make it more accessible and pretty. Example:
Anyway, hope that helps.
django-registration-redux 具有自定义信号,可用于将注册用户分配到特定组。 “user_registered”信号可用于此目的。请参阅 django-registration-redux 文档
django-registration-redux has custom signals that can be used to assign registered user to a specific group. "user_registered" signal can be used for this purpose. Please refer to django-registration-redux documentation