Django模板未加载
Posted
技术标签:
【中文标题】Django模板未加载【英文标题】:Django Template not loading 【发布时间】:2015-11-11 14:44:24 【问题描述】:我有一个名为“src”的项目和一个名为“app”的应用程序,并且我的应用程序中有一个模板文件夹。在我的模板文件夹中,我有另一个名为 pages 的文件夹,而我的 html 页面(base.html 和 view.html)就在那里。我的 view.py 在下面
from django.shortcuts import render
from django.utils import timezone
# Create your views here.
def home(request):
return render(request, "pages/base.html", )
和 urls.py
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.views.generic import TemplateView
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'$', 'app.views.home', name='home'),
url(r'^view$', TemplateView.as_view(template_name ='pages/view.html'), name = 'go'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_URL)
我通过单击 base.html 中的按钮调用 view.html。
base.html
% load staticfiles %
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title>Parasol.</title>
<!-- Bootstrap core CSS -->
<link href="% static 'css/bootstrap.min.css' %" rel="stylesheet">
<link href="% static 'css/navbar-static-top.css' %" rel="stylesheet">
<link href="% static 'css/style.css' %" rel="stylesheet">
<link href="navbar-static-top.css" rel="stylesheet">
<script src="../../assets/js/ie-emulation-modes-warning.js"></script>
</head>
<body>
<!-- Static navbar -->
<nav class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Parasol.</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Photos <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="../navbar/">Timeline</a></li>
<li class="active"><a href="./">Quotes<span class="sr-only">(current)</span></a></li>
<li><a href="../navbar-fixed-top/">Friends</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Just her and the revolution</h1>
<p>
<!-- <a href="view.html" class="btn btn-lg btn-primary">Lets take a ride.. »</a> -->
<a href="% url 'go' %" class="btn btn-lg btn-primary">Let's go </a>
<!-- <a href="/view/" class="btn btn-lg btn-primary">Lets take a ride.. »</a> -->
</p>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="../../dist/js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>
和 view.html
% load staticfiles %
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Chameleon Guys</title>
</head>
<body>
Its just for you dudes!!
</body>
</html>
但是当我点击按钮时,即使 url 相应地改变了 view.html 页面也没有加载。 http://0.0.0.0:8000/view
。我对 Django 有点陌生。请帮帮我。
【问题讨论】:
它有错误吗? 没有。它只是加载我的 base.html 页面本身 【参考方案1】:您的主视图匹配所有 URL,因为您已将其设置为匹配 $
,即“任何结束的字符串”,即所有这些。将其更改为:
url(r'^$', 'app.views.home', name='home'),
这样它只匹配空字符串。
【讨论】:
太棒了!!非常感谢丹尼尔。真的很感激!以上是关于Django模板未加载的主要内容,如果未能解决你的问题,请参考以下文章