【发布时间】:2021-08-22 16:54:24
【问题描述】:
我正在使用 Python 3.9 和
Django==3.1.4
djangorestframework==3.12.2
我想将一个安静的参数(“作者”字符串)传递给我的 GET 方法。在我的 urls.py 文件中,我有
urlpatterns = [
...
path('user/<str:author>', views.UserView.as_view()),
然后在我的 UserView 类(在我的 views.py 文件中定义)中,我有
class UserView(APIView):
def get(self, request):
...
author = self.kwargs.get('author', None)
但是当我执行时
GET http://localhost:8000/user/myauthor
我得到了错误
TypeError: get() got an unexpected keyword argument 'author'
我还需要做什么才能正确访问 URL 中的 RESTful 参数?
【问题讨论】:
标签: django django-rest-framework parameters restful-url