我正在使用Django Rest Framework(Django版本=4.0.4,我知道我需要升级,但我仍在学习诀窍和djangorestframework~=3.14.0)和Viewsets作为API,但我无法在列表页面上显示Browsable API POST表单。我可以看到ParameterKeys的列表(所以我认为urls.py和serializers.py是正确的),而且我使用的是超级用户帐户,所以我不认为这是权限问题。我的设置有什么问题?我确实注意到Brave浏览器代理请求有'text/html'和一些'application/xml',但没有'application/json'...不确定是不是这样?
以下是模型的相关部分:
class ParameterKey(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=200)
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name='parameter_keys')
def __str__(self):
return self.name
以下是www.example.com的相关部分urls.py:
from django.urls import path, include
from rest_framework import routers
from . import api_views
router = routers.DefaultRouter()
router.register(r'', api_views.ParameterViewSet)
urlpatterns = [
path('', include(router.urls)),
]
以下是API_views.py的相关部分:
from rest_framework import viewsets, parsers
from .models import ParameterValue, ParameterKey
from .serializers import ParameterValueSerializer, ParameterKeySerializer
from parameters.permissions import IsOwnerOrPublic
# from django.http import JsonResponse
# from django.views.decorators.csrf import csrf_exempt
from rest_framework.permissions import AllowAny
class ParameterKeyViewSet(viewsets.ModelViewSet):
permission_classes = [AllowAny]
queryset = ParameterKey.objects.all()
serializer_class = ParameterKeySerializer
这里是主项目urls.py:
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from datasets.views import DatasetListView, DatasetDetailView
from datasets.api_views import DatasetViewSet
from parameters.api_views import ParameterViewSet
from rest_framework import routers
from files import views
from files.views import upload_file
router = routers.DefaultRouter()
# router.register(r'datasets', DatasetViewSet, basename='dataset')
router.register(r'datasets', DatasetViewSet, basename='dataset')
router.register(r'parameterkeys', ParameterViewSet, basename='parameterkey') # Add this line
urlpatterns = [
# Django admin
path("anything-but-admin/", admin.site.urls),
# User management
path("accounts/", include("allauth.urls")),
# Include URLs from the datasets app
path("api/v1/datasets/", include("datasets.api_urls")),
# Include URLs from the parameters app
path("api/v1/parameters/", include("parameters.urls")),
# Non-API view
path("", include("datasets.urls")),
# API views
path("api/v1/", include(router.urls)),
# API authentication
path("api-auth/", include("rest_framework.urls")),
path("api/v1/dj-rest-auth/", include("dj_rest_auth.urls")),
path("api/v1/dj-rest-auth/registration/", include("dj_rest_auth.registration.urls")),
path('upload/', upload_file, name='upload_file'),
] + static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT
)
if settings.DEBUG:
import debug_toolbar
urlpatterns = [
path("__debug__/", include(debug_toolbar.urls)),
] + urlpatterns
以下是www.example.com的相关部分serializers.py:
from rest_framework import serializers
from .models import ParameterValue, ParameterKey
class ParameterKeySerializer(serializers.ModelSerializer):
class Meta:
model = ParameterKey
fields = '__all__'
我没有发布我的settings.py,因为SO将其标记为垃圾邮件。以下是已安装的应用程序:
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"whitenoise.runserver_nostatic", # new
"django.contrib.staticfiles",
"django.contrib.sites",
# Third-party
"rest_framework",
"crispy_forms",
"crispy_bootstrap5",
"allauth",
"allauth.account",
"debug_toolbar",
"corsheaders",
"rest_framework.authtoken",
"dj_rest_auth",
"storages",
# Local
"accounts.apps.AccountsConfig",
'datasets',
'parameters',
'files',
]
MIDDLEWARE = [
"django.middleware.cache.UpdateCacheMiddleware",
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware", # new
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"debug_toolbar.middleware.DebugToolbarMiddleware",
"django.middleware.cache.FetchFromCacheMiddleware",
]
下面是REST_FRAMEWORK设置:
REST_FRAMEWORK = {
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework.permissions.IsAuthenticatedOrReadOnly", # new
],
"DEFAULT_AUTHENTICATION_CLASSES": [ # new
"rest_framework.authentication.SessionAuthentication",
"rest_framework.authentication.TokenAuthentication",
],
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
],
}
我试过改变权限和不同的浏览器,我试过暂时禁用十字头。我相信问题出在某个地方的设置上,因为我的任何应用程序都没有在Browsable API中获得POST表单。当我写自己的视图时(即在我切换到视图集之前),我确实在不同的应用程序上有正确的表单。
1条答案
按热度按时间nkoocmlb1#
更新:我发现了。这是一个许可的事情。我删除了permission_classes=语句,现在表单出现了。