好吧,我以前也问过这个问题。但是,当我问它时,我对 python 和应用程序引擎的了解有限。这很可能是我过去未能实现这一点的原因。我正在尝试根据用户名使每个配置文件都是唯一的。
任何人,在我深入研究之前,我都会抛出我的个人资料处理程序,该处理程序目前只处理当前登录的用户:
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)
. 我已经搞砸了一段时间,但似乎尾随用户名(例如 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"
主处理器:
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)