Azure 上的 Django 应用程序没有获取静态文件 [英] Django app on Azure not getting static files
问题描述
在 Azure webapp 上获得了我的 Django 项目,但是当我在 SSH 终端上调用时:
Got my Django project on a Azure webapp, but when I call on SSH terminal:
Python manage.py collectstatic
Python manage.py collectstatic
它说复制了 252 个文件,但我的静态文件在我的模板中不可见,并且 wwwroot 中的静态文件夹为空...这是我的 wwwroot 结构:
It says 252 files copied but my static files are not visible on my templates and static folder in wwwroot its empty...Here's my wwwroot structure:
wwwroot
|---Myproject
|---manage.py
|---oryx-manifest.toml
|---hostingstart.html
|---static //With all my static files
├── myapp
│ ├── migrations
│ ├── __pycache__
│ ├── static
| | |---Images
| | | |--myimage.png
| | | |--myimage2.png
│ └── templates
这是我的 settings.py:
And this is my settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
('myapp', os.path.join(BASE_DIR, 'myapp', 'static')),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
知道为什么或我做错了什么吗?Azure 收集的不同吗?
Any idea why or what am I doing wrong ?, does Azure collect different ?
编辑>当我访问我的网站时,我的图片没有显示...我在模板上这样称呼它们:
EDIT> When I go to my website my images don´t show...Im calling them like this on template:
{% load static %}
<img src="{% static 'Images/myimage.png' %}" /><br>
编辑 2/////
在 wwwroot 中确实创建了一个包含我所有静态数据的文件夹,但是当我加载我的模板时它们不显示,在我的控制台中我得到 myimage.png 和 myimage2.png 这个错误:
In wwwroot creates indeed a folder with all my statics, but when I load my template they don´t show, in wen console I get this error for myimage.png and myimage2.png :
Failed to load resource: the server responded with a status of 404 (Not Found)
推荐答案
找到了!!
只需将这个:+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 添加到这样的 url 模式中:
Just had to add this: + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) to url patterns like this:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('myapp/', include('myapp.urls')),
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
它成功了,希望对其他人有帮助!
And it did the trick, hope it helps to anyone else!!
这篇关于Azure 上的 Django 应用程序没有获取静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!