Django 2.2 静态文件在开发中不起作用
Django 2.2 staticfiles do not work in development
我正在制作一个新的 Django 应用程序,用户可以在其中上传图像,然后它们可以显示在页面上。我已经阅读了 django 文档 10000 遍,但我仍然不明白为什么我的图像没有加载 (DEBUG = True)
我所有的图像都将到达它们应该在的位置,在我的模型的管理员中,django 有通往图像的正确路径,但它无法正确加载。
我在 settings.py 中的设置:
STATIC_URL = '/static/'
STATIC_ROOT = 'static' # live cdn such as AWS S3
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
MEDIA_ROOT = os.path.join(STATIC_ROOT, 'media')
MEDIA_URL = '/img/'
我的目录:
|- src
|- myproject
|- settings.py, views.py, urls.py etc
|- myapp
|- views.py urls.py models.py forms.py etc
|- static (this folder is empty and appeared after running collectstatic
|- templates
|- db.sqlite3
|- manage.py
|- static (this folder is OUTSIDE src)
|- admin
|- django admin stuff
|- media
|- img
|- myimage.png
在 myapp 的 models.py 中图像字段 upload_to = 'img/'
正如我所说,图片不会加载到页面上,而是会上传到 static/media/img/image_name.png
多年来我一直试图解决这个问题,但我无法从任何人那里得到任何帮助,django 文档和其他 Whosebug questions/answers 也没有任何帮助。
谢谢你的帮助!
Serving files uploaded by a user during development¶
During development, you can serve user-uploaded media files from
MEDIA_ROOT using the django.views.static.serve() view.
This is not suitable for production use! For some common deployment
strategies, see Deploying static files.
For example, if your MEDIA_URL is defined as /media/, you can do this
by adding the following snippet to your urls.py:
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)
STATICFILES_DIRS
表示 Django 将在哪里查找您的静态文件,这是您应该放置静态文件的地方,以便它们将在开发中提供,并在您部署时由 collectstatic 从中收集。
您似乎已将其设置为 src/static
,因此您应该将静态文件放在这里。
STATIC_ROOT
是在你拥有 运行 collectstatic
之后你的静态资产将被收集到的地方
STATICFILES_FINDERS
默认情况下会在 STATICFILES_DIRS
和每个 app/static
目录中查找
一个典型的布局是这样的
project_root/
project_name/
settings.py
urls.py
static/ < This is usually STATICFILES_DIRS
app_1/
models.py
views.py
static/ < AppDirectoriesFinder will look here
app_2/
models.py
views.py
static/ < AppDirectoriesFinder will look here
static/ < This is where assest will be collected by collectstatic
我遇到了同样的问题,事实上,我没有在 nginx.conf
中指定我的静态文件。
如果对你有帮助:有时我们会忘记检查我们的基本配置:D
我正在制作一个新的 Django 应用程序,用户可以在其中上传图像,然后它们可以显示在页面上。我已经阅读了 django 文档 10000 遍,但我仍然不明白为什么我的图像没有加载 (DEBUG = True)
我所有的图像都将到达它们应该在的位置,在我的模型的管理员中,django 有通往图像的正确路径,但它无法正确加载。
我在 settings.py 中的设置:
STATIC_URL = '/static/'
STATIC_ROOT = 'static' # live cdn such as AWS S3
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
MEDIA_ROOT = os.path.join(STATIC_ROOT, 'media')
MEDIA_URL = '/img/'
我的目录:
|- src
|- myproject
|- settings.py, views.py, urls.py etc
|- myapp
|- views.py urls.py models.py forms.py etc
|- static (this folder is empty and appeared after running collectstatic
|- templates
|- db.sqlite3
|- manage.py
|- static (this folder is OUTSIDE src)
|- admin
|- django admin stuff
|- media
|- img
|- myimage.png
在 myapp 的 models.py 中图像字段 upload_to = 'img/'
正如我所说,图片不会加载到页面上,而是会上传到 static/media/img/image_name.png
多年来我一直试图解决这个问题,但我无法从任何人那里得到任何帮助,django 文档和其他 Whosebug questions/answers 也没有任何帮助。
谢谢你的帮助!
Serving files uploaded by a user during development¶
During development, you can serve user-uploaded media files from MEDIA_ROOT using the django.views.static.serve() view.
This is not suitable for production use! For some common deployment strategies, see Deploying static files.
For example, if your MEDIA_URL is defined as /media/, you can do this by adding the following snippet to your urls.py:
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)
STATICFILES_DIRS
表示 Django 将在哪里查找您的静态文件,这是您应该放置静态文件的地方,以便它们将在开发中提供,并在您部署时由 collectstatic 从中收集。
您似乎已将其设置为 src/static
,因此您应该将静态文件放在这里。
STATIC_ROOT
是在你拥有 运行 collectstatic
STATICFILES_FINDERS
默认情况下会在 STATICFILES_DIRS
和每个 app/static
目录中查找
一个典型的布局是这样的
project_root/
project_name/
settings.py
urls.py
static/ < This is usually STATICFILES_DIRS
app_1/
models.py
views.py
static/ < AppDirectoriesFinder will look here
app_2/
models.py
views.py
static/ < AppDirectoriesFinder will look here
static/ < This is where assest will be collected by collectstatic
我遇到了同样的问题,事实上,我没有在 nginx.conf
中指定我的静态文件。
如果对你有帮助:有时我们会忘记检查我们的基本配置:D