我正在尝试为DRF设置媒体文件路径/图像,但是它不起作用,我无法弄清原因。
我收到此错误:
serve() got an unexpected keyword argument 'documuent_root'
我在使用python 3.6运行django 1.11 DRF的Mac上。
我已将设置网址移到了最高级别,这是为什么要使用此link的原因,所以虽然我仍然无法弄清楚为什么我在单击链接时会显示404,但我又走了一步。
settings.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
views.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')
urls.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)
答案 0 :(得分:1)
实际上是一个错字。您使用的是 documuent_root
,但是应该是 document_root
。
因此,请更改为
urlpatterns = [
.... other patters,
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)