新手,遇到问题搞半天,哭死。
问题大概是在本地运行,图片上传后,存到了相应的位置,但是返回的url却报404错误。
百度找了半天没有找到,google上面找到了,解决办法如下:
from django.contrib import admin
from django.urls import path, include,re_path
from rest_framework import routers
from rest_framework.documentation import include_docs_urls
from django.conf import settings
from django.conf.urls.static import static
from django.db.backends import sqlite3
from img.views import ImgVeiwSet
router = routers.DefaultRouter()
router.register(img', ImgVeiwSet,base_name='img')
urlpatterns = [
re_path(r'^admin/', admin.site.urls),
re_path(r'^docs/', include_docs_urls(title='img Api')),
re_path(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
re_path(r'^', include(router.urls))
]
# 这里是重点
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)