大家好,我开始意识到 DRF ModelViewSet 是一种制作休息视图的快速方法,我正在尝试使用路由器 url 中的参数,但 ViewSet 没有检测到参数
这是我的视图集的外观
class ClientRequests(ModelsViewSet):
serializer_class = serializers.ClientRequestSerializer
def get_queryset(self):
return models.ClientRequest.objects.filter(cliend_id=self.request.kwargs.get('client_id')
Run Code Online (Sandbox Code Playgroud)
现在我注册路由器如下
router = DefaultRouter()
router.register('/<int:client_id/requests', views.ClientRequests, basename='clients request api endpoint')
urlpatterns=[
path('', include(router.urls))
Run Code Online (Sandbox Code Playgroud)
这是使用 restframework 路由器的正确方法吗,以及如何在使用路由器时将参数传递给 url
使用前端 react 开发 django 应用程序,并使用 django 提供 react 构建文件。项目结构是因为在 settings.py 中一切正常
DEBUG=True
Run Code Online (Sandbox Code Playgroud)
但是,当我将其更改DEBUG=False
为控制台中的静态文件时,出现以下错误
来自“http://localhost:8000/static/js/2.285a2b2f.chunk.js”的资源由于 MIME 类型(“text/html”)不匹配(X-Content-Type-Options:nosniff)而被阻止。
来自“http://localhost:8000/static/css/main.dde91409.chunk.css”的资源由于 MIME 类型(“text/html”)不匹配(X-Content-Type-Options:nosniff)而被阻止。
由于 MIME 类型(“text/html”)不匹配(X-Content-Type-Options:nosniff),来自“http://localhost:8000/static/js/main.eade1a0b.chunk.js”的资源被阻止。
由于 MIME 类型(“text/html”)不匹配(X-Content-Type-Options:nosniff),来自“http://localhost:8000/static/css/2.03372da4.chunk.css”的资源被阻止。
来自“http://localhost:8000/static/js/2.285a2b2f.chunk.js”的资源由于 MIME 类型(“text/html”)不匹配(X-Content-Type-Options:nosniff)而被阻止。
在我的 setting.py 文件中,我还设置了以下内容
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'frontend')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'frontend', "build", "static"), # update the STATICFILES_DIRS
)
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
Run Code Online (Sandbox Code Playgroud)
一切正常,除非 DJango …