我需要让self.request.GET []拥有正确的代码,以便用户根据他们在HTML中点击的内容点击。
下面是Main.py:
import webapp2
from data import Fighter
from data import Data
from pages import Page
from pages import ContentPage
class MainHandler(webapp2.RequestHandler):
def get(self):
f = Fighter()
d = Data()
p = Page()
c = ContentPage()
if self.request.GET[1]:
self.response.write(c.results(d.fighter_data[0].name, d.fighter_data[0].rank, d.fighter_data[0].age, d.fighter_data[0].hometown, d.fighter_data[0].fights_out_of, d.fighter_data[0].height, d.fighter_data[0].weight, d.fighter_data[0].reach, d.fighter_data[0].wins, d.fighter_data[0].loses, d.fighter_data[0].bio))
else:
self.response.write(p.page)
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
如果self.request.GET [1]:如果点击?fighter = 1需要工作
下面的Pages.py:
class Page(object):
def __init__(self):
self.page = '''
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<a href="?fighter=1">Flweight</a>
<a href="?fighter=2">Bantamweight</a>
<a href="?fighter=3">Featherweight</a>
<a href="?fighter=4">Lightweight</a>
<a href="?fighter=5">Welterweight</a>
</body>
</html>'''
def content(self):
content_page = self.page # set content page equal to page
content_page = content_page.format(**locals()) # get locals
return content_page # return content_page
class ContentPage(object):
def __init__(self):
self.data_page = '''
{name} {age} {rank} {hometown} {fights_out_of} {height} {weight} {reach} {wins} {loses} {bio}'''
def results(self, name, age, rank, hometown, fights_out_of, height, weight, reach, wins, loses, bio): # get stuff from page
results_page = self.data_page # set results page equal to data page
results_page = results_page.format(**locals()) # get locals
return results_page # returns results_page
我感谢任何帮助。感谢。
答案 0 :(得分:0)
我已经解决了我自己的问题。它是:
if self.request.GET:
if self.request.GET['fighter']:
self.response.write(c.results(d.fighter_data[0].name, d.fighter_data[0].rank, d.fighter_data[0].age, d.fighter_data[0].hometown, d.fighter_data[0].fights_out_of, d.fighter_data[0].height, d.fighter_data[0].weight, d.fighter_data[0].reach, d.fighter_data[0].wins, d.fighter_data[0].loses, d.fighter_data[0].bio))
elif self.request.GET['fighter'] == 2:
self.response.write(c.results(d.fighter_data[1].name, d.fighter_data[1].rank, d.fighter_data[1].age, d.fighter_data[1].hometown, d.fighter_data[1].fights_out_of, d.fighter_data[1].height, d.fighter_data[1].weight, d.fighter_data[1].reach, d.fighter_data[1].wins, d.fighter_data[1].loses, d.fighter_data[1].bio))
elif self.request.GET['fighter'] == 3:
self.response.write(c.results(f.name, f.age, f.rank, f.hometown, f.fights_out_of, f.height, f.weight, f.reach, f.wins, f.loses, f.bio))
elif self.request.GET['fighter'] == 4:
self.response.write(c.results(f.name, f.age, f.rank, f.hometown, f.fights_out_of, f.height, f.weight, f.reach, f.wins, f.loses, f.bio))
elif self.request.GET['fighter'] == 5:
self.response.write(c.results(f.name, f.age, f.rank, f.hometown, f.fights_out_of, f.height, f.weight, f.reach, f.wins, f.loses, f.bio))
else:
self.response.write(p.page)