viewsets not working with ^ and $ added and invalid literal for int() with base 10: on string regex django rest framework

viewsets not working with ^ and $ added and invalid literal for int() with base 10: on string regex django rest framework

我有以下 url 是针对 djangorestframework

中的 viewsets
router.register(r'city-list', CityListViewSet, base_name='city-list')

如上所述 url 有效,但如果我这样做:

router.register(r'^city-list$', CityListViewSet, base_name='city-list')

它坏了,我收到 404 错误^ 是从头开始进行模式匹配的正则表达式,$ 类似于 ^ 但后面是模式匹配。

另外,看看这个 url:

router.register(r'venue-filter-options-list/(?P<city>[a-zA-Z]+)'

^$ 有同样的问题 我在 city 占位符

中输入字符串时出现错误

如果我在浏览器中调用 url 时将 chicago 放入 city 占位符

我在 Django 调试页面中收到以下错误:

Exception Type: ValueError

Exception Value:

invalid literal for int() with base 10: 'chicago'

这没有任何意义我的正则表达式是正确的。

还有其他人有这个问题吗?

  1. 视图集基于 Class,您需要在 url 中调用 .as_view()
  2. 您应该将方法指定为 as_view()
  3. 的字典
  4. ViewSet get默认需要pk字段进行查询,所以如果要使用其他的,需要在ViewSet声明中更改查找字段。该错误告诉您需要一个 int 值 the pk 而不是字符串。 从文档中阅读更多内容 http://www.django-rest-framework.org/api-guide/viewsets/