无法加载Django模板

时间:2015-08-17 13:36:31

标签: python django django-templates django-views django-generic-views

这里有一个奇怪的问题。我的应用程序中有两个名为app的模板。 base.htmlview.html。我的base.html页面中有一个按钮,可以导航到我的view.html。我面临的问题是当我点击base.html页面中的按钮时,view.html页面没有显示(即使在相应的网址中更改)。有人可以帮我识别问题吗?提前致谢。

我的文件夹和文件:

enter image description here

我的view.py

from django.shortcuts import render
from django.views.generic.detail import DetailView
from django.utils import timezone
from django.views import generic
def home(request):
    return render(request, "base.html", {})
class DetailView(generic.DetailView):
    template_name = 'app/view.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 app.views import DetailView
urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'$', 'app.views.home', name='home'),
    url(r'^view/', DetailView.as_view(), name='view'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_URL)

和我在base.html中的按钮

{% load staticfiles %}
{#<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>#}
<!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">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">
    <title>Parasol.</title>
      <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>
    <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>
          </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 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>
              </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>Heading Heading</h1>
        <p>
<a href="{% url 'view' %}" class="btn btn-lg btn-primary">Let's go</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>

被修改

根据Rekwan的新目录:

enter image description here

3 个答案:

答案 0 :(得分:0)

DetailView渲染一个&#34;细节&#34;一个对象的视图。 这是dev doc of cbv(class base view) 使用模板查看并尝试

答案 1 :(得分:0)

我认为问题与模板的位置有关。模板应该在templates/appname/templates/view.html中找到。它们应该由templates/view.html调用。在上面提供的代码段中,您有templates/appname/view.html

此外,您已导入DetailView,然后继续命名您的视图DetailView。我认为这会覆盖导入的类。将您的视图命名为泛型以外的其他内容,以避免潜在的错误。

希望有所帮助。

答案 2 :(得分:0)

答案如下。因为现在我的家庭视图正在寻找所有类型的角色。

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