flask restplus路径变量默认值

时间:2018-01-16 00:44:00

标签: swagger flask-restplus

如何使用Flask restplus设置路径变量的默认值?

例如,使用以下内容:

@api.route('/calendars/coordinates/<string:latitude>/<string:longitude>/years/<string:year>')
class DailyCalendarHandler(Resource):
  get_parser = reqparse.RequestParser()
  get_parser.add_argument('timezone', type=str, default='Asia/Calcutta', help='Example: Asia/Calcutta', location='args', required=True)
  get_parser.add_argument('encoding', type=str, default='devanagari', help='Example: iast, devanagari, kannada, tamil', location='args',
                          required=True)

  @api.expect(get_parser)
  def get(self, latitude, longitude, year):
    args = self.get_parser.parse_args()
    city = City("", latitude, longitude, args['timezone'])

如何设置年份,纬度和经度的默认值?

如果是请求变量,可以执行以下操作:

get_parser = reqparse.RequestParser()
get_parser.add_argument('output_transliteration', location='args', default='devanagari', help='devanagari/slp1/iast/hk/wx/itrans/kolkata/velthuis')

1 个答案:

答案 0 :(得分:0)

您可以在类上面定义多个URL规则,并定义参数的默认值。类似的东西:

@api.route('/calendars/coordinates', defaults={'latitude':123, 'longitude': 56})
@api.route('/calendars/latitude/<string:latitude>/longitude/<string:longitude>/years/<string:year>')
    class DailyCalendarHandler(Resource):

  def get(self, latitude, longitude, year='2017'):