Django模板未加载

时间:2015-08-18 15:11:13

标签: python html django django-templates django-urls

我有一个名为'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.. &raquo;</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.. &raquo;</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>

但是当我点击按钮时,即使网址相应更改,也不会加载view.html页面。 http://0.0.0.0:8000/view。对Django来说有点新鲜。请帮帮我。

1 个答案:

答案 0 :(得分:2)

您的主视图匹配所有网址,因为您已将其设置为匹配$,即“任何字符串结束”,即所有网址。将其更改为:

url(r'^$', 'app.views.home', name='home'),

这样它只匹配空字符串。