大家好几周以来我一直在玩webapp2和Python。简而言之,我进入了路由主题。 我发现了一个有用的link,可以了解如何完成路由......不知何故,在我的项目中,我一直在努力解决这个问题。
基本上我想做的就是添加国家/地区,然后在创建链接/ countries / country_id(其中country id应为iso3)时单独查看每个国家/地区,以便稍后使用jquery更新其属性可以编辑或删除它们。更好地称为CRUD
我可以在这里得到任何帮助吗?这可能是一个非常愚蠢的解决方案......我的大脑没有做对......: - (
我一直这样:
INFO 2015-04-06 15:33:49,839 module.py:737]默认值:" GET / HTTP / 1.1" 405 187
这是我的处理程序代码:
class HomePage(BaseRequestHandler):
def get(self):
self.render_template('index.html')
class CountriesHandler(BaseRequestHandler):
def create(self):
"""I want to get this from https://github.com/mledoze/countries
Could this be possible to get it from json...? to be researched
"""
country_key = ndb.Key(Country, self.request.get('ccn3'))
country = Country((country_key),
name = self.request.get('name'),
ccn3 = self.request.get('ccn3'),
region = self.request.get('region'),
)
country.put()
"""I might be doing wrong here..."""
self.redirect('/countries/country')
self.render_template('add_country.html')
def list(self, country_key):
self.render_template('list.html')
def view(self, country_key):
"""I think I have to do something here to get thinks to work with routing
"""
self.render_template('country.html')
def update(self, ndb_key):
"""This should update one single field"""
if type(ndb_key) is ndb.key.Key:
key = ndb_key
else:
key = ndb.Key(urlsafe=ndb_key)
item = key.get()
item.self.request.get('value')
item.put()
def delete(self, ndb_key):
"""This should delete an entity"""
if type(ndb_key) is ndb.key.Key:
key = ndb_key
else:
key = ndb.Key(urlsafe=ndb_key)
key.delete()
这是我的路由器代码:
"""Here i get always confused which one I should use and how I should use"""
_route_info = [
('Main', 'GET', '/', HomePage, 'home'),
('country.list', 'GET', '/countries/', CountriesHandler, 'list'),
('country.create', None, '/countries/create/', CountriesHandler, 'create'),
('country.view', 'GET', '/countries/<id:\d+>/', CountriesHandler, 'view'),
('country.delete', None, '/countries/<id:\d+>/delete/', CountriesHandler, 'delete'),
('country.update', None, '/countries/<id:\d+>/update/', CountriesHandler, 'update'),
]
更新1 :
我稍微更改了代码。 但仍然得到同样的错误:
class CountriesHandler(BaseRequestHandler):
def create(self):
if self.request.method == 'POST':
country_key = ndb.Key(Country, self.request.get('ccn3'))
country = Country((country_key),
name = self.request.get('name'),
ccn3 = self.request.get('ccn3'),
region = self.request.get('region'),
)
country.put()
return self.redirect(self.uri_for('country.view'))
else:
return self.render_template('putcountry.html')
答案 0 :(得分:1)
使用Route类。像这样:
_route_info = [
webapp2.Route('/countries', CountriesHandler, name='country.list', methods=['GET'], handler_method='list')
]
完整文档here
答案 1 :(得分:0)
这是一个有趣的问题,因为没有内置映射。我选择了带有国家/地区名称和城市名称的手动地图数据存储区ID,以便为我的网络应用实现网址,如<country>/<city>/<category>
。我最终想要跟随地图城市和国家。
includes:
- br.yaml # Brazil
- in.yaml # India
然后在in.yaml
handlers:
- url: /(india/for_sale|goa.*|delhi.*|gujarat.*|haryana.*|andaman.*|andhra_pradesh.*|arunachal_pradesh.*|uttar_pradesh.*|uttaranchal.*|tripura.*|tamil_nadu.*|sikkim.*|rajasthan.*|punjab.*|manipur.*|meghalaya.*|mizoram.*|nagaland.*|orissa.*|madhya_pradesh.*|kerala.*|karnataka.*|jharkhand.*|jammu_kashmir.*|himachal_pradesh.*|west_bengal.*|assam.*|bihar.*|chhattisgarh.*|maharashtra.*|pondicherry.*|lakshadweep.*|daman_diu.*|dadra.*|dadra_nagar_haveli.*|chandigarh.*|andaman_nicobar_islands.*|sindh.*)
script: in.app
- url: /india/jobs.*
script: india_jobs.application
- url: /india/cars-for_sale.*
script: india_cars-for_sale.application
- url: /india/real_estate.*
script: india_real_estate.application