404找不到静态文件-Django [英] 404 Static file not found - Django
问题描述
我对django有问题.我最近买了一个共享服务器的实例,我想将django网站从AWS移到该服务器(使用Cpanel).在AWS上一切正常,但是当我切换到Cpanel时,所有静态文件都丢失了.
I have an issue with 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). All worked fine with AWS but when I switched to Cpanel all statics files were missing.
这是我的设置.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
|-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>
这是错误:
GET http://my.domain.com/static/main_page/js/my-script.js net::ERR_ABORTED 404 (Not Found)
当我转到文件的URL时,它像我的URL之一一样理解它:
When I go to the URL of the file it understands it like one of my URLs:
希望您能帮助我解决此问题;)谢谢.
I hope you will help me to solve this issue ;) thanks.
推荐答案
,您需要添加静态&像这样在urls.py中配置媒体文件
you need to add the static & media files config in the urls.py , like this
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)
django文档: https://docs.djangoproject.com/zh/3.1/howto/static-files/
the django docs : https://docs.djangoproject.com/en/3.1/howto/static-files/
这篇关于404找不到静态文件-Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!