通过 Django 后端的身份验证创建时将用户标记为新用户
我有一个基于旧数据库的身份验证后端。当有人使用该数据库登录并且没有相应的 User
记录时,我会创建一个记录。我想知道是否有某种方法可以提醒 Django 系统注意这一事实,例如我可以将全新用户重定向到不同的页面。
我唯一能想到的就是在用户的个人资料记录中添加一个名为 is_new
的标志,该标志会测试一次,然后在它们被测试后立即设置为 False
重定向。
基本上,我想知道其他人是否已经解决了这个问题,这样我就不必重新发明轮子了。
I have an authentication backend based off a legacy database. When someone logs in using that database and there isn't a corresponding User
record, I create one. What I'm wondering is if there is some way to alert the Django system to this fact, so that for example I can redirect the brand-new user to a different page.
The only thing I can think of is adding a flag to the users' profile record called something like is_new
which is tested once and then set to False
as soon as they're redirected.
Basically, I'm wondering if someone else has figured this out so I don't have to reinvent the wheel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现完成此任务的最简单方法就是完全按照您所说的进行。我对我的一个项目也有类似的要求。我们需要向任何新会员显示“不要忘记更新您的个人资料”消息,直到他们第一次访问他们的个人资料。客户希望尽快获得它,因此我们在用户的个人资料中添加了一个“visited_profile”字段,并将其默认为 False。
我们决定这样做是因为它的实施速度非常快,不需要修改注册过程,与现有用户合作,并且不需要每次页面加载时进行额外的查询(因为已经在每个页面上检索用户和用户个人资料) )。我们花了 10 分钟来添加字段、运行 South 迁移并将 if 标记放入模板中。
I found the easiest way to accomplish this is to do exactly as you've said. I had a similar requirement on one of my projects. We needed to show a "Don't forget to update your profile" message to any new member until they had visit their profile for the first time. The client wanted it quickly so we added a 'visited_profile' field to the User's profile and defaulted that to False.
We settled on this because it was super fast to implement, didn't require tinkering with the registration process, worked with existing users, and didn't require extra queries every page load (since the user and user profile is retrieved on every page already). Took us all of 10 minutes to add the field, run the South migration and put an if tag into the template.
据我所知,有两种方法可以确定对象是否已创建:
1) 使用
get_or_create
时,会返回(obj,created)
形式的元组,其中created
是一个布尔值,清楚地表明该对象是否已创建2)
post_save
信号传递一个created
参数,也是一个布尔值,也指示是否创建对象是否已创建。在最简单的层面上,您可以使用这两个钩子中的任何一个来设置会话变量,然后您可以相应地检查和重定向。
如果您可以使用它,您也可以在调用
get_or_create
后或在post_save
信号中直接重定向。There's two methods that I know of to determine if an object has been created:
1) When using
get_or_create
a tuple is returned of the form(obj, created)
wherecreated
is a boolean indicating obviously enough whether the object was created or not2) The
post_save
signal passes acreated
paramater, also a boolean, also indicating whether the object was created or not.At the simplest level, you can use either of these two hooks to set a session var, that you can then check and redirect accordingly.
If you can get by with it, you could also directly redirect either after calling
get_or_create
or in thepost_save
signal.您可以使用基于文件的缓存来存储尚未保存到数据库的用户。当用户第二次登录时,可以在缓存中查找,找到用户对象,并将其永久保存到数据库中。
以下是有关 django 缓存的一些信息: http://docs.djangoproject.com /en/dev/topics/cache/?from=olddocs
PS:不要使用Memcached,因为它会在计算机崩溃或关闭的情况下删除所有信息。
You can use a file-based cache to store the users that aren't yet saved to the database. When the user logs in for the second time, you can look in the cache, find the user object, and save it to the database for good.
Here's some info on django caching: http://docs.djangoproject.com/en/dev/topics/cache/?from=olddocs
PS: don't use Memcached because it will delete all information in the situation of a computer crash or shut down.