django css不会从索引网址加载,也不会从其他任何网址加载

时间:2020-07-19 10:35:17

标签: django django-templates django-staticfiles

为什么CSS从索引网址而不是其他网址加载?

网址:

    urlpatterns = [
    path("", views.index, name="index"),
    path("product/<slug>", views.product, name="product"),] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
#+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

views.py:

def index(request):
return render(request, "product.html")

def product(request, slug):
    product = Product.objects.get(slug='iphone-11')
    print(product.image1.url)
    context = {'product': product}
    return render(request, "product.html", context)

product.html:

{%load static%}

<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Nakahaty</title>
<meta name="keywords" content="HTML5 Template">
<meta name="description" content="Molla - Bootstrap eCommerce Template">
<meta name="author" content="p-themes">
<!-- Favicon -->
<link rel="apple-touch-icon" sizes="180x180" href="static/assets/images/icons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="static/assets/images/icons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="static/assets/images/icons/favicon-16x16.png">
<link rel="manifest" href="static/assets/images/icons/site.html">
<link rel="mask-icon" href="static/assets/images/icons/safari-pinned-tab.svg"

color =“#666666”>

编辑:当我从索引URL加载相同的HTML文件时,它将起作用,否则它将不会加载CSS文件

ps:不使用表格

{% static ..%}

3 个答案:

答案 0 :(得分:1)

您需要像这样定义静态网址。

{% load static %}
<link rel="manifest" href=" {% static 'static/assets/images/icons/site.html' %}">

答案 1 :(得分:1)

您的静态文件路径不正确。看起来应该像这样:

{% load static %}
<link rel="apple-touch-icon" sizes="180x180" href="{% static 'assets/images/icons/apple-touch-icon.png' %}">```

答案 2 :(得分:1)

如果使用Django,请使用Django:

<link rel="apple-touch-icon" sizes="180x180" href="{% static 'assets/images/icons/apple-touch-icon.png' %}">
<link rel="icon" type="image/png" sizes="32x32" href="{% static 'assets/images/icons/favicon-32x32.png' %}">
<link rel="icon" type="image/png" sizes="16x16" href="{% static 'assets/images/icons/favicon-16x16.png' %}">
<link rel="manifest" href="{% static 'assets/images/icons/site.html' %}">
<link rel="mask-icon" href="{% static 'assets/images/icons/safari-pinned-tab.svg' %}">
<link rel="stylesheet" href="{% static 'your css folder/ your css file name' %}

已阅读有关this

的信息