2

我试图为 DRF 设置媒体文件路径/图像,但它不起作用,我不知道为什么。

我收到此错误:

在此处输入图像描述

serve() got an unexpected keyword argument 'documuent_root'

我在 Mac 上运行 django 1.11 DRF w/python 3.6。

我已将设置 url 移动到顶级,为什么通过此链接的方式,所以我更近了一步,尽管我仍然无法弄清楚为什么我的链接在单击它们时显示 404。

设置.py

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'src')

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

CORS_ORIGIN_WHITELIST = 'localhost:3000', #whitelists the localhost to run

视图.py

from accounts.api.permissions import IsOwnerOrReadOnly
from rest_framework import generics, mixins, permissions, viewsets
from books.models import Books
from books.api.serializers import BooksSerializer


class BookViewSet(viewsets.ModelViewSet):
    permission_classes      = [permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly]    # authentication_classes  = []
    serializer_class        = BooksSerializer  # necessary

    queryset                = Books.objects.all()
    lookup_field            = 'id'
    search_fields           = ('user__username', 'content', 'user__email')
    ordering_fields         = ('user__username', 'timestamp')

网址.py

from django.conf.urls import url, include
from django.contrib import admin
from . import views
from django.conf.urls.static import static
from django.conf import settings
from rest_framework import routers
from books.api.views import (
                BookViewSet)

router = routers.SimpleRouter()
router.register(r'books', BookViewSet) # --> http://127.0.0.1:8000/api/books/api/books/


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^api/', include(router.urls)),

  ] + static(settings.MEDIA_URL, documuent_root=settings.MEDIA_ROOT)
4

1 回答 1

6

它实际上是一个错字。你正在使用documuent_root,但它应该是document_root

所以,改为

urlpatterns = [
    .... other patters,
    ]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

于 2018-08-29T04:33:26.217 回答