gpt4 book ai didi

python - [django]当debug = false时,MEDIA_URL返回找不到

转载 作者:行者123 更新时间:2023-12-01 03:11:30 34 4
gpt4 key购买 nike

当 DEBUG=TRUE 时,media_url 有效,但 DEBUG = False 时,返回无效。
这是我的设置文件。

设置.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/")

也许,这个设置是推荐的 Debug模式。
我应该如何更改此设置。

最佳答案

根据the documentation :

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;
}

关于python - [django]当debug = false时,MEDIA_URL返回找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42861326/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com