问题描述
我有一个简单的django项目。我要在其中添加材料设计引导程序。
settings.py:
STATIC_URL = '/static/'
# _______________________________________________________________________
STATIC_ROOT = os.path.join(BASE_DIR,'/assets')
# _______________________________________________________________________
STATICFILES_Dirs = [
os.path.join(BASE_DIR,'/static')
]
urls.py:
from .views import index,core,other
urlpatterns = [
path('',index,name='startpage'),path('core/',name = 'homepage'),]
views.py:
from django.shortcuts import render
def index(request):
return render(request,'index.html')
def core(request):
return render(request,'base.html')
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8" />
<Meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no" />
<Meta http-equiv="x-ua-compatible" content="ie=edge" />
<title>Material Design for Bootstrap</title>
<!-- MDB icon -->
<link rel="icon" href="{% static 'assets/img/mdb-favicon.ico' %}" type="image/x-icon" />
<!-- Font Awesome -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.11.2/css/all.css" />
<!-- Google Fonts Roboto -->
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap"
/>
<!-- MDB -->
<link rel="stylesheet" href="{% static 'assets/css/mdb.min.css' %}" >
<!-- Custom styles -->
<style></style>
</head>
<body>
<!-- Start your project here-->
<header>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-light bg-white">
<div class="container-fluid">
<button
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarExample01"
aria-controls="navbarExample01"
aria-expanded="false"
aria-label="Toggle navigation"
>
<i class="fas fa-bars"></i>
</button>
<div class="collapse navbar-collapse" id="navbarExample01">
<ul class="navbar-nav mr-auto mb-2 mb-lg-0">
<li class="nav-item active">
<a class="nav-link" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- Navbar -->
<!-- Jumbotron -->
<div class="p-5 text-center bg-light">
<h1 class="mb-3">heading</h1>
<h4 class="mb-3">Subheading</h4>
<a class="btn btn-primary" href="" role="button">Call to action</a>
</div>
<!-- Jumbotron -->
</header>
<!-- End your project here-->
</body>
<!-- MDB -->
<script type="text/javascript" src="js/mdb.min.js"></script>
<!-- Custom scripts -->
<script type="text/javascript"></script>
</html>
base.html:
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8">
<title>My MDB Django Project</title>
{% load static %}
<!-- Font Awesome -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- Bootstrap -->
<link href="{% static 'assets/css/bootstrap.min.css' %}" rel="stylesheet">
<!-- Material Design for Bootstrap -->
<link href="{% static 'assets/css/mdb.min.css' %}" rel="stylesheet">
</head>
<body>
<div style="height: 100vh">
<div class="flex-center flex-column">
<h1 class="animated fadeIn mb-4">MDBootstrap loves Django <i class="fa fa-heart red-text"></i></h1>
</div>
</div>
<!-- jQuery -->
<script src="{% static 'assets/js/jquery-3.3.1.min.js' %}"></script>
<!-- Popper.js -->
<script src="{% static 'assets/js/popper.min.js' %}"></script>
<!-- Bootstrap -->
<script src="{% static 'assets/js/bootstrap.min.js' %}"></script>
<!-- Material Design for Bootstrap -->
<script src="{% static 'assets/js/mdb.min.js' %}"></script>
<script>
new WOW().init();
</script>
</body>
</html>
系统规格:
ubuntu 18.04
python 3.7
django 2.1.15
标头-Bootstrap 5 & Material Design 2.0 navigation
我通过docker-compose在本地运行项目:
sudo docker-compose up
当我尝试打开此网址时:
我收到此错误:
Error during template rendering In template /NowruzWebsite/core/templates/index.html,error at line 10
Invalid block tag on line 10: 'static'. Did you forget to register or load this tag?
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <Meta charset="UTF-8" />
5 <Meta name="viewport" content="width=device-width,shrink-to-fit=no" />
6 <Meta http-equiv="x-ua-compatible" content="ie=edge" />
7 <title>Material Design for Bootstrap</title>
8
9 <!-- MDB icon
-->
10 <link rel="icon" href="{% static 'assets/img/mdb-favicon.ico' %}" type="image/x-icon" />
11
12 <!-- Font Awesome -->
13 <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.11.2/css/all.css" />
14 <!-- Google Fonts Roboto -->
15 <link
16 rel="stylesheet"
17 href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap"
18 />
19 <!-- MDB -->
20 <link rel="stylesheet" href="{% static 'assets/css/mdb.min.css' %}" >
跟踪:
环境:
请求方法:GET请求URL:http://127.0.0.1:8000/core/
Django版本:2.1.15 Python版本:3.7.9安装的应用程序: ['django.contrib.admin','django.contrib.auth', 'django.contrib.contenttypes','django.contrib.sessions', 'django.contrib.messages','django.contrib.staticfiles','core'] 已安装的中间件: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware']
模板错误:在模板/NowruzWebsite/core/templates/index.html中, 第10行出现错误第10行的块标记无效:“静态”。你是否 忘记注册或加载此标签? 1:2:
3:4: 5:6:7: 自举的材料设计8:9:10:11: 12:13: 14:15:19:20:跟踪:
文件“ /usr/local/lib/python3.7/site-packages/django/template/base.py” 在解析中 470. compile_func = self.tags [command]
在处理上述异常(“静态”)期间,另一个异常 发生:
文件 “ /usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py” 在内部 34. response = get_response(request)
文件 “ /usr/local/lib/python3.7/site-packages/django/core/handlers/base.py” 在_get_response中 126. response = self.process_exception_by_middleware(e,request)
文件 “ /usr/local/lib/python3.7/site-packages/django/core/handlers/base.py” 在_get_response中 124. response = wrapd_callback(request,* callback_args,** callback_kwargs)
索引中的文件“ /NowruzWebsite/core/views.py” 5.返回render(request,'index.html')
文件“ /usr/local/lib/python3.7/site-packages/django/shortcuts.py”位于 渲染 36. content = loader.render_to_string(模板名称,上下文,请求,使用=使用)
文件 在“ /usr/local/lib/python3.7/site-packages/django/template/loader.py”中 render_to_string 61. template = get_template(template_name,using = using)
文件 在“ /usr/local/lib/python3.7/site-packages/django/template/loader.py”中 get_template 15.返回engine.get_template(template_name)
文件 “ /usr/local/lib/python3.7/site-packages/django/template/backends/django.py” 在get_template中 34. return Template(self.engine.get_template(template_name),self)
文件 在“ /usr/local/lib/python3.7/site-packages/django/template/engine.py”中 get_template 144. template,origin = self.find_template(template_name)
文件 在“ /usr/local/lib/python3.7/site-packages/django/template/engine.py”中 find_template 126. template = loader.get_template(name,skip = skip)
文件 “ /usr/local/lib/python3.7/site-packages/django/template/loaders/base.py” 在get_template中 30.内容,来源,origin.template_name,self.engine,
文件“ /usr/local/lib/python3.7/site-packages/django/template/base.py” 在初始化中 156. self.nodelist = self.compile_nodelist()
文件“ /usr/local/lib/python3.7/site-packages/django/template/base.py” 在compile_nodelist中 194. return parser.parse()
文件“ /usr/local/lib/python3.7/site-packages/django/template/base.py” 在解析中 472. self.invalid_block_tag(令牌,命令,parse_until)
文件“ /usr/local/lib/python3.7/site-packages/django/template/base.py” 在invalid_block_tag中 534.“ or load this tag?” %(token.lineno,命令)
异常类型:/ core /处的TemplateSyntaxError异常值:无效 第10行的代码块:“静态”。您忘记注册或加载了吗 这个标签?
解决方法
请检查此代码段。这些设置对我来说可以显示HTML图片。
文件夹结构
Settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [os.path.join(BASE_DIR,'templates')],'APP_DIRS': True,'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages',],},]
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),)
urls.py项目
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/',admin.site.urls),path('',include('igapp.urls')),]
urls.py应用
from django.urls import path
from . import views
urlpatterns = [
path('',views.home,name="home"),]
views.py
from django.shortcuts import render
def home(request):
return render(request,"index.html")
index.html
{%load static%}
<html>
<head><title>Sample</title></head>
<body>
<h3>Hello World</h3>
<img src="{% static 'images/shop.png' %}" />
</body>
</html>