我对Django静态文件有问题.
让我们假设Django被配置为100处的服务器.
当我的配置为:
STATIC_URL = "/static/"
MEDIA_URL = "/media/"
STATIC_ROOT = "/home/hostxxx/domains/webpage.com/DjangoApp/static"
MEDIA_ROOT = "/home/hostxxx/domains/webpage.com/DjangoApp/media"
Then Django looks for:
https://example.com/static/styles/style.css
但它并不存在:
GET https://example.com/static/styles/style.css net::ERR_ABORTED 404 (Not Found)
个
但该文件位于以下位置:
https://example.com/app/static/styles/style.css
个
但当我更改静态url时:
STATIC_URL = "/app/static/"
Then it looks for a good url:
https://example.com/app/static/styles/style.css
个
but now the staticfile is available here:
https://example.com/app/app/static/styles/style.css
最新情况:
urls.py:
from django.contrib import admin
from django.urls import path
from mainapp import views
urlpatterns = [
path("admin/", admin.site.urls),
path("", views.lists, name="lists"), # MAIN VIEW
]
但也try 了将其作为urls.py:
from django.contrib import admin
from django.urls import path
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from mainapp import views
urlpatterns = [
path("admin/", admin.site.urls),
path("", views.lists, name="lists"),
]
urlpatterns += staticfiles_urlpatterns()
和base.html:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>AAA</title>
<link rel="stylesheet" type="text/css"
href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css" />
<link rel="stylesheet" href="{% static 'styles/style.css' %}"> # HERE
...
主机使用Passenger_wsgi.py,即:
import imp
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
wsgi = imp.load_source("wsgi", "app/wsgi.py")
application = wsgi.application
django-admin --version
:5.0
.
python --version
:Python 3.10.13
.
The solution offered on every other topic:
FORCE_SCRIPT_NAME = "/app"
does not work. Django template is still searching for: https://example.com/static/styles/style.css
and not for: https://example.com/app/static/styles/style.css
个.
我读了很多答案,试了很多次,都没有用.
它工作得很好,当我设置Django项目没有应用程序前缀:100,但我有其他应用程序在那里服务,我需要添加前缀.
I don't understand, why it cannot generate a valid link for css:
The app/
part is still missing.
我应该如何正确配置Django静态文件来服务和搜索此css? https://example.com/app/static/styles/style.css个