Django网址似乎不起作用

时间:2015-08-02 15:48:33

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

您好我有一个名为mysite的django项目。 并在内部创建一个名为app的应用程序。当我运行服务器时,我的base.html似乎正在工作。但是,当我点击主页面(base.html)中的链接按钮时,应该能够加载另一个html文件。我有以下观点

`def view(request):
    return render(request,"view.html", {})

在我的views.py和网址

url(r'^admin/', include(admin.site.urls)),
url(r'$', 'app.views.home', name='home'),
url(r'^view/', 'app.views.view', name='view'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_URL)`

我的链接按钮是

<a href="view.html" class="btn btn-lg btn-primary">Lets take a ride.. &raquo;</a>

模板: 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>

    <!-- 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">

    <!-- Custom styles for this template -->
    <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>
          </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="#">One more separated link</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">

      <!-- Main component for a primary marketing message or call to action -->
      <div class="jumbotron">
        <h1>Blah Blah</h1>
        <p>
<a href="{% url 'view' %}" class="btn btn-lg btn-primary">Let's go</a>
        </p>
      </div>

    </div> <!-- /container -->
    <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>`

即使我的webbrowser上的url显示view.html,页面也没有加载。请帮帮我吧。谢谢。

3 个答案:

答案 0 :(得分:2)

您正在使用已命名的网址,因此只需使用该功能:

<a href="{% url 'view' %}" class="btn btn-lg btn-primary">Let's go</a>

有关详细信息,请参阅documentation

答案 1 :(得分:1)

创建模板目录。

finally

尝试在您的settings.py路径上添加。

-proj
    -app
        -templates
                  -view.html

答案 2 :(得分:0)

在href值内部放置来自urls.py的网址。

<a href="/view/" class="btn btn-lg btn-primary">Lets take a ride.. &raquo;</a>