简体   繁体   English

404 未找到静态文件 - Django

[英]404 Static file not found - Django

I have an issue with django.我有 Django 的问题。 I recently bought an instance of a shared server and I wanted to move my django website from AWS to this server (which use Cpanel).我最近买了一个共享服务器的实例,我想将我的 django 网站从 AWS 移动到这个服务器(使用 Cpanel)。 All worked fine with AWS but when I switched to Cpanel all statics files were missing.在 AWS 上一切正常,但是当我切换到 Cpanel 时,所有静态文件都丢失了。

this is my settings.py:这是我的settings.py:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = "/media/"

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

my project structure:我的项目结构:

my_project
|-app/
    |-...
    |-views.py
|-db.sqlite3
|-manage.py
|-media/
|-my_project/
    |-...
    |-settings.py
|-static/
    |-main_page/
        |-js/
            |-my-script.js

I add static files like this:我添加这样的静态文件:

{% load static %}
<script src="{% static 'main_page/js/my-script.js' %}"></script>

This is the error:这是错误:

GET http://my.domain.com/static/main_page/js/my-script.js net::ERR_ABORTED 404 (Not Found)

When I go to the URL of the file it understands it like one of my URLs:当我转到文件的 URL 时,它会像我的 URL 之一一样理解它:

404错误截图

I hope you will help me to solve this issue ;) thanks.我希望你能帮助我解决这个问题 ;) 谢谢。

you need to add the static & media files config in the urls.py , like this您需要在 urls.py 中添加静态和媒体文件配置,如下所示

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] 
urlpatterns  +=  static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

the django docs : https://docs.djangoproject.com/en/3.1/howto/static-files/ django 文档: https : //docs.djangoproject.com/en/3.1/howto/static-files/

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

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