django - 如何定义 APIView 的 url?
问题描述
我正在尝试使用 Django Rest Framework 制作 APIView。当我将视图与 url 关联时,出现此错误:
AssertionError: basename argument not specified, and could not automatically determine the name from the viewset, as it does not have a .queryset attribute.
这是我的 APIView :
class ExampleView(APIView):
authentication_classes = (SessionAuthentication, BasicAuthentication)
permission_classes = (IsAuthenticated,)
def get(self, request, format=None):
content = {
'user': unicode(request.user), # `django.contrib.auth.User` instance.
'auth': unicode(request.auth), # None
}
return Response(content)
和路由器:
router = routers.DefaultRouter()
router.register('api/example', views.ExampleView.as_view())
那么,怎么了?谢谢 !
解决方案
您只需将路径添加到您的urlpatterns
. 路由器与视图集一起使用。
from django.urls import path
app_name = 'example'
urlpatterns = [
path('api/example', views.ExampleView.as_view(), name='example')
]
推荐阅读
- java - 将整数转换和存储为 ASCII 字符
- jenkins-pipeline - 无法在管道脚本中使用 Jenkins 环境变量
- flutter - Visual Studio Code 显示的错误是真实的吗?
- elasticsearch - 如何运行需要 Elasticsearch 身份验证的节拍容器
- python - 文件未正确从 json 转换为 jsonl
- python - 如何比较数据框中同一列的数据(熊猫)
- svg - d3js 缩放和平移排除标记
- powershell - 如何从 powershell 激活 conda 环境?
- python - Python:根据第一列中的值提取excel单元格值
- firebase - Flutter + SharedPreferences:如何使用 FutureBuilder