【问题标题】:page will keep getting 404 error with {%endfor%} but if removed will render but now posts will show页面将继续使用 {%endfor%} 出现 404 错误,但如果删除将呈现,但现在将显示帖子
【发布时间】:2021-07-26 09:41:28
【问题描述】:

嗨,我希望我在这里说得通我希望有人可以为我指出为什么 {% 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%} 来结束块,但如果我删除它,我看不到任何帖子

【问题讨论】:

  • 显示的代码没有for循环。

标签: python django django-views


【解决方案1】:
{% for post in posts %}
    {{ post.title }}
    {{ post.date_posted }}
   ... 
{% endfor %}

这应该可行。 您正在使用 {{ endfor }},但您没有从 for 循环开始。

【讨论】:

  • 谢谢亲,我会及时通知你
  • 如果它对您进行了排序,则接受为正确答案。
猜你喜欢
  • 2021-09-13
  • 1970-01-01
  • 1970-01-01
  • 2020-07-21
  • 2012-03-26
  • 2021-11-28
  • 2017-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多