django css 不会从索引 url 而不是任何其他 url 加载
Posted
技术标签:
【中文标题】django css 不会从索引 url 而不是任何其他 url 加载【英文标题】:django css doesn't load from the index url and not any other url 【发布时间】:2020-11-08 17:53:37 【问题描述】:为什么 css 从索引 url 加载而不是任何其他 url?
网址:
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 ..%
【问题讨论】:
【参考方案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
【讨论】:
【参考方案2】:你需要像这样定义静态 url。
% load static %
<link rel="manifest" href=" % static 'static/assets/images/icons/site.html' %">
【讨论】:
【参考方案3】:您的静态文件路径不正确。它应该看起来像:
% load static %
<link rel="apple-touch-icon" sizes="180x180" href="% static 'assets/images/icons/apple-touch-icon.png' %">```
【讨论】:
以上是关于django css 不会从索引 url 而不是任何其他 url 加载的主要内容,如果未能解决你的问题,请参考以下文章
Django 'resolve':获取 url 名称而不是 view_function
如何使用 Django 功能而不是所有键来索引刚刚选择的 json 键?
Django REST 框架——如何将 ForeignKey 解析为实际值,而不是索引?