2012-06-29 82 views
0

可能重複:
create unique profile page for each user python創建permenant獨特聯繫

我使用谷歌的AppEngine Python和Jinja2的,並試圖給我的應用程序唯一的每個用戶沒有登錄的任何人都可以訪問他們的個人資料頁的URL。以下是我的代碼到目前爲止:

class ProfilePage(webapp2.RequestHandler): 
    def get(self, profile_id): 
    user = User.get_by_id(profile_id) 
    #profile_id = some unique field 
    if user: 
     #Get all posts for that user and render.... 
     theid = user.theid 
     personalposts = db.GqlQuery("select * from Post where theid =:1 order by created desc limit 30", theid) 
    else: 
     personalposts = None 
    global visits 
    logout = users.create_logout_url(self.request.uri) 
    currentuser = users.get_current_user() 
    self.render('profile.html', user = currentuser, visits = visits, logout=logout, personalposts=personalposts) 

app = webapp2.WSGIApplication([('/', MainPage), 
           ('/profile/([0-9]+)', ProfilePage),]) 

當我嘗試並測試它時,它只是給我一個404錯誤。我想如果代碼是正確的,我可能會使用錯誤的測試URL。例如,如果這是他們的OpenID ID:我如何測試它我試着進入www.url.com/profile/ https://www.google.com/accounts/o8/id?id=AItOawlILoSKGNwU5RuTiRtXug1l8raLE45g-56只是id =「這部分」是我放的,所以我會:

url = www.url.com/profile/AItOawlILoSKGNwU5RuTiRtXug1l8raLE45g-56 

這就是我的嘗試,它並沒有很好的工作。先謝謝您的幫助!

回答

1

您正在使用(www.url.com/profile/ AItOawlILoSKGNwU5RuTiRtXug1l8raLE45g-56)鏈接的最後部分是一個完整的關鍵實體,而在你的代碼使用實體ID可加載的網址它(使用get_by_id())。

+0

它不是一個祕密的URL,我希望它是公開的。這就是全部原因,這樣每個人都可以看到它 – exployre

+0

我在那裏做什麼?我不太清楚這意味着什麼...... – exployre

1

試試這個正則表達式:

r'^/profile/\w*?-(\d+)$' 

雖然我還必須告訴你,這是一個非常糟糕的主意!

+0

是不是很糟糕,因爲任何人都可以把任何東西放在那裏? – exployre

+0

您正在使用參數化查詢,所以這不是問題。只是不要在其他地方使用該值(例如,不要將其回顯到頁面中)。 – Amber