{%endfor%} 页面将继续出现 404 错误,但如果删除将呈现但现在帖子将显示

时间:2021-05-04 02:33:36

标签: python django django-views

嗨,我希望我在这里说得通我希望有人能指出我为什么 {% endfor % 对我造成 404 错误但是当我删除它时页面呈现但没有帖子显示 提前谢谢你,我还附上了截图

视图代码

    from django.shortcuts import render
from django.http import HttpResponse


posts = [
{

   'author':  'Joshua Harvey',
   'title':    'blog post',
   'content':   'first post test',
   'date':       'may 1st 2021',
   
},
{

   'author':  ' Deidre gibson',
   'title':    'blog post 2',
   'content':   'second post test',
   'date':       'may 2st 2021',
   
}


]



def myView(request):
    context = {
          'posts': posts
    }
    return render(request, 'hello/home.html', context)
def about(request):
    return render(request, 'hello/about.html', {'title':about})

home.html 的代码

{% extends "hello/base.html" %}
{% block content %}
        <article class="media content-section">
                <article class="media content-section">
                    <div class="media-body">
                    <div class="article-metadata">
                        <a class="mr-2" href="#">{{ post.author }}</a>
                        <small class="text-muted">{{ post.date_posted }}</small>
                    </div>
                    <h2><a class="article-title" href="#">{{ post.title }}</a></h2>
                    <p class="article-content">{{ post.content }}</p>
                    </div>
                </article>
          
             
          {% endblock content %}

当我在 {% endblock content%} 上方输入 {%endfor} 时,网页出现 404 错误,但我需要 {%endfor%} 来结束块,但如果我删除它,我看不到任何帖子

1 个答案:

答案 0 :(得分:1)

{% for post in posts %}
    {{ post.title }}
    {{ post.date_posted }}
   ... 
{% endfor %}

这应该有效。 您正在使用 {{ endfor }} 并且您没有从 for 循环开始。