通过URL的唯一用户配置文件

时间:2012-08-07 00:06:50

标签: python google-app-engine

好吧,我过去曾问过这个问题。然而,当我问它时,我对python和app引擎的了解有限。这很可能是我过去未能实现这一点的原因。我试图根据用户名让每个配置文件都是唯一的。

任何人,在我潜入之前,我会抛出我的个人资料处理程序,目前只处理当前登录的用户:

class Profile(MainHandler):
    def get(self):
        if self.user:
            current_user = str(self.user.name)
            key = ''
            imgs  = db.GqlQuery("select * from Profile_Images WHERE name =:1", current_user)
            team_name  = db.GqlQuery("select * from Teams WHERE name =:1", current_user)
            team_images  = db.GqlQuery("select * from Teamimg WHERE user =:1", current_user)
        for clan in team_name:
                name1 = clan.team_name_anycase
        for image in team_images:
            team_imagee = image.key()
        if self.user:
            for img in imgs:
                key = img.key()
            self.render('profile.html', team_img = team_imagee, team_name = name1, profile_image = key, username = self.user.name, email = self.user.email, firstname = self.user.first_name, last_name = self.user.last_name, country = self.user.country)
        else:
            self.redirect('/register')

此处理程序通过('profile',Profile)进行映射。

到目前为止,我理解的是,我需要通过URL传递用户名并进入配置文件处理程序,然后使用该用户名作为标识符从数据库中提取数据。

我在stackoverflow上发布的内容是('profile/<profile_id>', Profile)。我一直在搞乱这一点,但似乎尾随用户名(ex.localhost:8080 / profile / admin)收到404错误。我会假设我的映射失败,或者变量(即用户名)无法与配置文件处理程序交互。

有人可以帮帮我吗?我确信我有它,但它失败了。

YAML文件:

application: suitegamer
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /static
  static_dir: static

- url: /img
  static_dir: img

- url: /.*
  script: main.app

libraries:
- name: jinja2
  version: latest

- name: PIL
  version: "1.1.7"

MainHandler:

class MainPage(MainHandler):
    def get(self):
    if self.user:
            self.render('index.html', username = self.user.name, firstname = self.user.first_name)
        else:
            self.render('index.html')

    def post(self):
    username = self.request.get('username').lower()
        password = self.request.get('password')

        u = User.login(username, password)
        if u:
            self.login(u)
            self.redirect('/news_page')
        else:
            msg = 'Invalid login.'
            self.render('login.html', error = msg)

映射:

app = webapp2.WSGIApplication([('/', MainPage),
                               ('/logout', Logout),
                               ('/img', GetImage),
                               ('/register', Register),
                               ('/welcome', Welcome),
                               ('/news_page', News_Page),
                               ('/profile', Profile),
                               ('/edit_profile', Edit_Profile),
                               ('/change_profile_image', Change_Profile_Image),
                               ('/found_a_team', Found_Team),
                               ('/team_main', Team_Main),
                               ('/edit_team_main', Edit_Team_Main),
                               ('/edit_team_image', Edit_Team_Image)],
                              debug=True)

测试配置文件处理程序:

class Profile(MainHandler):
    def get(self, profile_id):
        profile_id = 'admin'
        if self.user:
            key = ''
            imgs  = db.GqlQuery("select * from Profile_Images WHERE name =:1", profile_id)
            team_name  = db.GqlQuery("select * from Teams WHERE name =:1", profile_id)
            team_images  = db.GqlQuery("select * from Teamimg WHERE user =:1", profile_id)
        for clan in team_name:
                name1 = clan.team_name_anycase
        for image in team_images:
            team_imagee = image.key()
        if self.user:
            for img in imgs:
                key = img.key()
            self.render('profile.html', team_img = team_imagee, team_name = name1, profile_image = key, username = self.user.name, email = self.user.email, firstname = self.user.first_name, last_name = self.user.last_name, country = self.user.country)
        else:
            self.redirect('/register')

图像处理程序:

class GetImage(MainHandler):
    def get(self):
        img = db.get(self.request.get("entity_id"))
        self.response.out.write(img.image)

映射到('/ img',GetImage)

1 个答案:

答案 0 :(得分:1)

映射应如下所示:

('/profile/(.*)/?', Profile)

并且您的处理程序获取函数应该采用一个参数,该参数将是配置文件ID /名称

class Profile(MainHandler):
    def get(self, profile_id):
        # do something with the profile id...

我认为你应该更深入地了解文档 例如:https://developers.google.com/appengine/docs/python/tools/webapp/running