python - webapp2 CRUD 路由返回 405

标签 python google-app-engine url-routing crud webapp2

嗨,几周以来我一直在使用 webapp2 和 Python。简而言之,我进入了路由主题。 我发现了一个有用的link这给出了如何完成路由的想法...不知何故,在我的项目中,我正在努力解决这个问题,但没有看到任何成功。

基本上我想做的就是添加国家/地区,然后在创建它们时通过链接/countries/country_id(其中国家/地区 id 应该是 iso3)单独查看每个国家/地区,稍后能够使用 jquery 更新其属性xeditable 或删除它们。更广为人知的名称是 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')

最佳答案

使用 Route 类。像这样:

_route_info = [
  webapp2.Route('/countries', CountriesHandler, name='country.list', methods=['GET'], handler_method='list')
]

完整文档 here

关于python - webapp2 CRUD 路由返回 405,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29474716/

相关文章:

python - 在一列上对数据框进行分组,并从一列中获取最大值,并从另一列中获取相应的值

Python falcon 和异步操作

java - 适用于多个 api 类的 Google Endpoints Android 客户端库

google-app-engine - Google App Engine 加载请求非常频繁

mod-rewrite - 两个 mod 重写规则

python - 使用任何脚本语言(perl、python 或 shell 脚本)查找范围内的缺失值

python - Google App Engine 和 Cloud SQL : Lost connection to MySQL server at 'reading initial communication packet' SQL 2nd Gen

grails - 如何使我的URL映射不区分大小写?

reactjs - GitHub Pages 不为项目提供服务,而是呈现根用户站点

Python 2.7 : Saving a plot from pandas