简体   繁体   English

Django 2.2 静态/媒体文件未显示/处理

[英]Django 2.2 static / media files not showing / processing

I'm trying to deploy my Django instructor's (Udemy) 'real estate' project.我正在尝试部署我的 Django 讲师 (Udemy) 的“房地产”项目。 Here is Brad Traversy's gist . 这是 Brad Traversy 的要点

On my VPS I'm running Ubuntu 18.04, Python 3.6.8 and Django 2.2.4.在我的 VPS 上,我运行的是 Ubuntu 18.04、Python 3.6.8 和 Django 2.2.4。

I've followed Brad's guide for deploying Django.我已经按照 Brad 的指南来部署 Django。 I kinda got it to run but the static files (CSS, JS and images) are not being served.我有点让它运行,但没有提供静态文件(CSS、JS 和图像)。 It's an issue with my configuration and it is not clear to me what I'm doing wrong here or what exactly I am missing.这是我的配置问题,我不清楚我在这里做错了什么或者我到底缺少什么。

Here is what the end result should look like:最终结果应该是这样的:

在此处输入图片说明

Here is what mine actually looks like:这是我的实际外观:

在此处输入图片说明

Exploring other questions and answers on SO, inside my settings.py, I've tried swapping in (and out) different paths into STATIC_ROOT, MEDIA_ROOT, STATICFILES_DIRS using the join method.探索关于 SO 的其他问题和答案,在我的 settings.py 中,我尝试使用 join 方法将不同的路径换入(和换出)到 STATIC_ROOT、MEDIA_ROOT、STATICFILES_DIRS 中。 I've also tried adding a number of different list items into the urlpatterns variable inside urls.py.我还尝试将许多不同的列表项添加到 urls.py 中的 urlpatterns 变量中。 After trying each new potential change, I enter python manage.py collectstatic and then I run the server.在尝试每个新的潜在更改后,我输入python manage.py collectstatic然后运行服务器。 Sometimes there would be a trace back preventing the server from running.有时会有回溯阻止服务器运行。 In these situations, I just rolled back the change to a setting which was known to work or alternatively, I just tried something else entirely which would enable the server to run without a trace back.在这些情况下,我只是将更改回滚到已知可以工作的设置,或者,我只是尝试了其他完全可以使服务器运行而没有回溯的方法。

As per @IrrupTor's answer, when running this project locally, the static and media files parse perfectly.根据@IrrupTor 的回答,在本地运行此项目时,静态文件和媒体文件可以完美解析。 So this confirms the issue exists in my remote configuration on my VPS.所以这证实了我的 VPS 上的远程配置中存在问题。 What other information could I provide to better clarify why static and media files are not loading on my VPS?我可以提供哪些其他信息来更好地阐明为什么我的 VPS 上没有加载静态和媒体文件?

Here are some of the other questions on SO which I have used already:以下是我已经使用过的关于 SO 的其他一些问题:

Here is the output in my Django shell:这是我的 Django shell 中的输出:

python manage.py runserver 0.0.0.0:8000
Performing system checks...

System check identified no issues (0 silenced).
September 14, 2019 - 00:44:11
Django version 2.2.4, using settings 'btre.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[14/Sep/2019 00:44:15] "GET / HTTP/1.1" 200 13775
[14/Sep/2019 00:44:15] "GET /static/css/bootstrap.css HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/css/all.css HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/css/style.css HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/css/lightbox.min.css HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/js/jquery-3.3.1.min.js HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/img/logo.png HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/js/lightbox.min.js HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/js/bootstrap.bundle.min.js HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/js/main.js HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/js/lightbox.min.js HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/js/main.js HTTP/1.1" 404 77

Here is my urls.py:这是我的 urls.py:

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', include('pages.urls')),
    path('listings/', include('listings.urls')),
    path('accounts/', include('accounts.urls')),
    path('contacts/', include('contacts.urls')),
    path('admin/', admin.site.urls),

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Here are the relevant lines in my settings.py:以下是我的 settings.py 中的相关行:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'btre/static')
]

# Media Folder Settings
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

Here is my file tree:这是我的文件树:

在此处输入图片说明

Here is my (forked) source code repo on GitHub .这是我在 GitHub 上的(分叉的)源代码存储库

One final note: Right now I am using Django's test server on my VPS.最后一点:现在我在我的 VPS 上使用 Django 的测试服务器。 I realize this is not best practices because it is not secure.我意识到这不是最佳实践,因为它不安全。 I am merely deploying with the Django's built in server as a stepping stone.我只是将 Django 的内置服务器作为垫脚石进行部署。 Once the static and media files load I'll promptly switch to nginx and gunicorn, as described in the original guide.加载静态和媒体文件后,我会立即切换到 nginx 和 gunicorn,如原始指南中所述。

I tried to reproduce your error:我试图重现您的错误:

It was working fine with settings.py configuration,使用 settings.py 配置工作正常,

But as I added the static url configuration in urls.py then I also received 404 error.但是当我在 urls.py 中添加静态 url 配置时,我也收到了 404 错误。

I would suggest to modify to urls.py to following:我建议将 urls.py 修改为以下内容:

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', include('pages.urls')),
    path('listings/', include('listings.urls')),
    path('accounts/', include('accounts.urls')),
    path('contacts/', include('contacts.urls')),
    path('admin/', admin.site.urls),

]

I figured it all out.我想通了。

At the bottom of settings.py in my original configuration, I attempted to import separate production settings with this:在我的原始配置中 settings.py 的底部,我尝试使用以下内容导入单独的生产设置:

try:
    from .local_settings import *
except ImportError:
    pass

Take note of the period before local_settings .记下local_settings之前的local_settings This would refer to the hidden .local_settings.py file in the same directory.这将引用同一目录中隐藏的 .local_settings.py 文件。 In my actual configuration, my production settings file was not actually hidden.在我的实际配置中,我的生产设置文件实际上并未隐藏。 My production settings just existed in the project directory as local_settings.py.我的生产设置作为 local_settings.py 存在于项目目录中。 There was actually no period prefixed.实际上没有句号前缀。 Given that my production settings were not referenced properly, this was my problem.鉴于我的生产设置没有被正确引用,这是我的问题。

I really should have included the details of my production settings config file in my original question.我真的应该在我的原始问题中包含我的生产设置配置文件的详细信息。 I am not sure how I missed this.我不确定我是如何错过这个的。

My production DEBUG variable setting was set to False yet my Django live environment was still showing debug information in my web browser when I attempted to run my server.我的生产DEBUG变量设置被设置为False但当我尝试运行我的服务器时,我的 Django 实时环境仍然在我的 Web 浏览器中显示调试信息。 So this should have clued me in but I missed that last weekend when I first assembled this Stack Overflow question.所以这应该给我提供了线索,但上周末我第一次组装这个 Stack Overflow 问题时我错过了。

For now I got the server to run and parse static and media files properly but with changes in my original settings.py.现在,我让服务器运行并正确解析静态和媒体文件,但更改了我的原始 settings.py。 So I'm still not calling local_settings.py properly.所以我仍然没有正确调用 local_settings.py。 I'll write a separate question about how to properly invoke production settings later.我稍后会写一个关于如何正确调用生产设置的单独问题。

Here are the temporary changes that I made to the primary settings.py which is working for me now:以下是我对主要settings.py所做的临时更改,它现在对我有用:

  1. ALLOWED_HOSTS includes my server's IP address. ALLOWED_HOSTS 包括我服务器的 IP 地址。
  2. My DATABASES dictionary refers to the basic testing SQLite configuration instead of the more advanced PostgreSQL.我的 DATABASES 字典指的是基本的测试 SQLite 配置,而不是更高级的 PostgreSQL。
  3. Instead of STATIC_ROOT set to os.path.join(BASE_DIR, 'static') , I have it set to os.path.join(os.path.dirname(BASE_DIR), "static")而不是 STATIC_ROOT 设置为os.path.join(BASE_DIR, 'static') ,我将它设置为os.path.join(os.path.dirname(BASE_DIR), "static")

I also changed the trailing urlpatterns static function call to include this: + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) .我还更改了尾随 urlpatterns 静态函数调用以包含以下内容: + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) However whether I comment out or comment in that line, the server seems to continue to parse static and media files without issue.但是,无论我在该行中注释还是注释,服务器似乎都可以继续解析静态和媒体文件而不会出现问题。

不,.local_settings 并不意味着您需要有一个“.local_settings.py”文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM