0

Google App Engine で webapp2 を使用してアプリケーションを構築しています。ユーザー名を URL に渡して、プロファイル ボタンをクリックすると、ユーザーが「/profile/username」に移動するようにするにはどうすればよいですか。「username」はユーザー固有のものですか?

私の現在のハンドラ:

app = webapp2.WSGIApplication([('/', MainPage),
                               ('/signup', Register),
                               ('/login', Login),
                               ('/logout', Logout),
                               ('/profile', Profile)
                               ],
                              debug=True)

プロファイル クラス:

class Profile(BlogHandler):
    def get(self):
        email = self.request.get('email')
        product = self.request.get('product')
        product_list = db.GqlQuery("SELECT * FROM Post ORDER BY created DESC LIMIT 10")
        self.render('profile.html', email = email, product = product, product_list = product_list)

データベースに固有の情報を含むプロファイル ページに各ユーザーを送信しようとしています。ありがとう

4

3 に答える 3

2

考えられる解決策の 1 つは、単純に 1 つの URL、つまり/profile. 対応するハンドラーは、ログインしたユーザーからのデータを使用して応答をレンダリングします。

のような URL が本当に必要な場合は、ルート/profile/usernameを定義できます。

app = webapp2.WSGIApplication([('/', MainPage),
                               ('/signup', Register),
                               ('/login', Login), 
                               ('/logout', Logout),
                               ('r/profile/(\w+)', Profile)
                              ],
                              debug=True)

ハンドラーでユーザー名にアクセスします。

class Profile(BlogHandler):
    def get(self, username):

/profile/usernameただし、アプリケーションによっては、ハンドラーのどこかにチェックを追加して、ログインしているユーザーだけがアクセスできるようにしたい場合があります。

于 2012-07-25T15:46:49.700 に答える
0

http://webapp-improved.appspot.com/guide/routing.htmlを参照してください

あなたは次のようなものを持つことができます

class Profile(BlogHandler):
  def get(self, username):
    ...

app = webapp2.WSGIApplication([('/profile/(\w+)', Profile), ...])
于 2012-07-25T15:40:00.967 に答える
0

/profileにキャプチャグループを追加することから始めます。

(r'/profile/(\w+)', Profile)

文字列の先頭のr前は、正規表現の文字を正しく処理するため、重要です。それ以外の場合は、ブラックスラッシュを手動でエスケープする必要があります。

\w+1つ以上の英数字とアンダースコアに一致します。ユーザー名はこれで十分ですよね?

次に、RequestHandlerを次のように設定します。

class Profile(webapp2.RequestHandler):
    def get(self, username):
        # The value captured in the (\w+) part of the URL will automatically
        # be passed in to the username parameter. 
        # Do the rest of my coding to get and render the data.
于 2012-07-25T15:41:26.960 に答える