我在本地服務器上運行,當我在瀏覽器中訪問http://127.0.0.1:8000/media/時,它說未找到頁面。MEDIA_URL Page 404錯誤Django
設置:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
根URL:
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^polls/', include('mysite.polls.urls')),
url(r'^admin/', admin.site.urls),
url(r'^submit/', include('mysite.uploads.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
模型用戶上傳:
from django.db import models
class Document(models.Model):
description = models.CharField(max_length=255, blank=True)
document = models.FileField(upload_to='documents/')
uploaded_at = models.DateTimeField(auto_now_add=True)
繼Django文檔:通過上傳
放送的檔案用戶在開發過程中
在開發過程中,您可以使用django.contrib.staticfiles.views.serve()視圖爲MEDIA_ROOT上傳用戶上傳的媒體文件。
這不適合生產使用!對於一些常見的部署策略,請參閱部署靜態文件。
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
https://docs.djangoproject.com/en/1.10/howto/static-files/
'http://127.0.0.1:8000/media /'的404不是問題。您需要嘗試查看媒體文件,例如通過'http://127.0.0.1:8000 /媒體/ image.jpg'。 – Alasdair