[django]当 debug=false 时,MEDIA_URL returns 未找到
[django]when debug=false,MEDIA_URL returns not found
当 DEBUG=TRUE 时,media_url 工作,但 DEBUG = False 时,returns 不工作。
这是我的设置文件。
setting.py
DEBUG = False
...
MEDIA_URL = "/pics/"
MEDIA_ROOT = BASE_DIR
urls.py
urlpatterns = [
....
....
] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
home.html
...
<img src="{{ post.image.url}}" ..>
models.py
class Post(models.Model):
title = models.CharField(max_length=255)
pub_date = models.DateTimeField()
image = models.ImageField(upload_to="media/")
也许,这个设置是推荐的调试模式。
我应该如何更改此设置。
This helper function works only in debug mode and only if the given
prefix is local (e.g. /media/) and not a URL (e.g.
http://media.example.com/).
他们提到的辅助函数是:+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
在生产环境中为 nginx 设置静态和媒体文件非常简单,DigitalOcean has a great guide。静态部分只有几行:
location /media/ {
root /home/sammy/myproject;
}
将下面的代码设置为 "urls.py" 以在 "DEBUG = False" 中显示媒体文件:
# "urls.py"
from django.conf.urls import url
from django.views.static import serve
from django.conf import settings
urlpatterns = [
# ...
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
]
当 DEBUG=TRUE 时,media_url 工作,但 DEBUG = False 时,returns 不工作。
这是我的设置文件。
setting.py
DEBUG = False
...
MEDIA_URL = "/pics/"
MEDIA_ROOT = BASE_DIR
urls.py
urlpatterns = [
....
....
] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
home.html
...
<img src="{{ post.image.url}}" ..>
models.py
class Post(models.Model):
title = models.CharField(max_length=255)
pub_date = models.DateTimeField()
image = models.ImageField(upload_to="media/")
也许,这个设置是推荐的调试模式。
我应该如何更改此设置。
This helper function works only in debug mode and only if the given prefix is local (e.g. /media/) and not a URL (e.g. http://media.example.com/).
他们提到的辅助函数是:+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
在生产环境中为 nginx 设置静态和媒体文件非常简单,DigitalOcean has a great guide。静态部分只有几行:
location /media/ {
root /home/sammy/myproject;
}
将下面的代码设置为 "urls.py" 以在 "DEBUG = False" 中显示媒体文件:
# "urls.py"
from django.conf.urls import url
from django.views.static import serve
from django.conf import settings
urlpatterns = [
# ...
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
]