django - 为什么帖子不显示在主页上?
问题描述
当我在管理面板中添加帖子时,帖子不会显示在应用程序的主页上。它只显示帖子的空格,但无法在主页上显示帖子的文章。
视图.py
from django.shortcuts import render,HttpResponseRedirect
from django.contrib import messages
from django.http import HttpResponse
from .forms import SignUpForm, LoginForm
from django.contrib.auth import authenticate, login, logout
from .models import Post
# Create your views here.
# home
def home(request):
posts = Post.objects.all()
return render(request, 'blog/home.html',{'posts':posts})
主页.html
{% extends 'blog/base.html' %}
{% load static %}
{% block content %}
<div class="col-sm-10">
<h3 class="text-white my-5">Home Page</h3>
{% for post in posts %}
<div class="jumbotron jumbotron-fluid jumbo-colour">
<div class="container">
<h1 class="display-4 font-weight-bold">{{posts.title}}</h1>
<p class="lead">{{posts.desc}}</p>
</div>
</div>
{% endfor %}
</div>
{% endblock content %}
模型.py
from django.db import models
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=150)
desc = models.TextField()
解决方案
您的代码中有错字。它应该是:
{% for post in posts %}
<div class="jumbotron jumbotron-fluid jumbo-colour">
<div class="container">
<h1 class="display-4 font-weight-bold">{{post.title}}</h1> // not posts.title
<p class="lead">{{post.desc}}</p> // not posts.desc
</div>
</div>
{% endfor %}
推荐阅读
- java - 使用不同帐户的 Google 登录在应用程序中崩溃
- laravel - 发送电子邮件并以 laravel 形式获得回复
- python - 如何打破while循环
- performance - 为什么动画代码和下一个动画帧之间有这么长的时间?
- regex - 两位数下一个点两位数下一个点两位数两个字母在单行文本字段中使用正则表达式
- web - PWA 在某些国家/地区受到限制?喜欢中国?
- scala - 如何以特定的时间间隔从服务器向客户端发送 Web 套接字消息?
- mysql - Mysql搜索组或字符串中的特定单词
- java - 在 jdk 1.8.0 编译器中从用户获取输入值
- angular - Vmmem 在 1GB 内存使用时崩溃