在Heroku上的Django中模板渲染的麻烦

时间:2017-08-22 01:03:45

标签: python django heroku

我通过了一个非常好的教程(Django Girls)。在Django的本地服务器上完美运行。

只要网页是静态的,它也可以在Heroku上完美运行。但是,一旦我使用模板填充三个简单的项目,它就不会像在本地Django服务器上那样呈现它们。

我一直在安装/使用whitenoise,遵循Heroku网站上的建议等等。所有这一切都是因为管理控制台看起来很漂亮(它已经像一个网页一样呈现非常低的带宽 - 没有实际的图形魅力,只有带下划线的文本)。因此,使用whitenoise似乎在那里有效。

但是在我的家庭酿造页面上,当我查看从Heroku发送到浏览器的HTML时,它包含所有各种标签等,但没有任何内容。

正如我所说的那样,因为它在本地服务器上工作正常。我已经尝试更改{%include static%} tp {%include staticfiles%},我已经运行了' collectstatics'成功,我已插入所有whitenoise和heroku推荐的代码。

模板非常简单:

{% load staticfiles %}
<html>
<head>
  <title> My Blog</title>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0>/css/bootstrap.min.css">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="{% static 'css/blog.css' %}">
</head>
<body>
  <div>
    <h1><a href="/">My Test Blog</a></h1>
  </div>
  <div class="content container">
    <div class="row">
      <div class="col-md-8">
         {% for post in posts %}
            <div class="post">
              <h2<a href="">{{post.title}}</a></h2>
              <div class="date"><p>published: {{post.published_date }}</p></div>
              <p>{{ post.text|linebreaksbr|linebreaksbr }}</p>
            </div>
         {% endfor %}
       </div>
     </div>
   </div>
 </body>
</html>

调用代码传入一个好的字典,因为它在本地渲染得很好。然而,Heroku上的远程实现所带来的东西已经被剥离了#39;模板中的信息很多,但不是全部。它看起来像这样:

<html>
  <head>
    <title>My Blog</title>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="/static/css/blog.css">
</head>

<body>
  <div>
    <h1><a href="/">My Test Blog</a></h1>
  </div>
<div class="content container">
    <div class="row">
        <div class="col-md-8">

        </div>
      </div>
    </div>
</body>
</html>

如果你想看到它,这就是本地网络服务器的内容,所有这些都完全正确:

<html>
  <head>
    <title>My Blog</title>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="/static/css/blog.css">
</head>

<body>
  <div>
    <h1><a href="/">My Test Blog</a></h1>
  </div>
<div class="content container">
    <div class="row">
        <div class="col-md-8">

      <div class="post">
        <h2<a href="">Here is post 1</a></h2>
        <div class="date"><p>published: Aug. 21, 2017, 4:44 p.m. </p></div>
            <p>This is the etxt for the first post</p>
          </div>

      <div class="post">
        <h2<a href="">2nd post</a></h2>
        <div class="date"><p>published: Aug. 21, 2017, 4:44 p.m. </p></div>
            <p>If one is not enough, here is #2...</p>
          </div>

      <div class="post">
        <h2<a href="">3rd post</a></h2>
        <div class="date"><p>published: Aug. 21, 2017, 4:44 p.m. </p></div>
            <p>And yet one more....</p>
          </div>

        </div>
      </div>
    </div>
</body>
</html>

显然,它获得了相同的调用代码,但是如果您想知道:

from django.shortcuts import render
from django.utils import timezone
from .models import Post

def post_list(request) :
    posts =     Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request,'blog/post_list.html', {'posts' : posts})

通常一个人怀疑版本不一致,但使用requirements.txt以及virtualenv似乎可以消除大部分问题。管理控制台的信息看起来完全相同,所以它有点奇怪,因为人们会怀疑Django本身在这两个平台上不会有完全不同的行为,特别是在完成Heroku为使用whitenoise所做的所有步骤之后, Heroku否则收集静态或运行代码没有问题 - 它肯定会找到模板但无法填写它。

0 个答案:

没有答案
相关问题