创造一个新的美国

2024-04-29 10:03:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个基本的用户模型:

class Song(db.Model):
    title = db.StringProperty()
    lyrics = db.TextProperty()
    singer = db.StringProperty()

class UserProfile(db.Model):
    user = db.UserProperty()
    song = db.ReferenceProperty(Song)

google appengine api提供了当前用户:

^{pr2}$

可以使用URL组合/注销:

^{3}$

如何确保用户是否注册到我的应用程序,如果他已注册,则获取与他对应的数据,如果他没有注册,请创建一个新的用户对象。 这将为他提供创建/编辑歌曲的功能。在


Tags: 用户模型singerdbmodelsongtitleclass
1条回答
网友
1楼 · 发布于 2024-04-29 10:03:39
user = users.get_current_user()
if user:
  # user is logged in, see if a profile exists ...
  profile = UserProfile.gql('WHERE user = :1', user).get()
  if not profile:
    # no profile exists - create it
    profile = UserProfile(user=user)
    profile.put()
  # do something with profile
else:
  # rediect to login
  self.redirect(users.create_login_url("/")))

相关问题 更多 >