会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132370个问题

一、项目压缩包如下

徐永波_2019.5.25_博客有问题代码.rar

二、项目存在问题简述如下:

  通过 年、月向数据库查询数据。无法查询到内容(数据库有两条数据,但是无法正常渲染到页面)

   blob.png

三、涉及到此用途的代码如下:

  i定义的方法如下:

class PostArchive(View):
    def get(self,request,year=1,month=1):
        #获取传递的参数并转换类型
        year = int(year)
        month = int(month)
    
        if year == 1 and month == 1:
            postobj = Post.objects.all().order_by('-publish_time')
        else:
            postobj = Post.objects.filter(publish_time__year=year,publish_time__month=month)

        return render(request,'listpost.html',{'postobj':postobj})

 ii渲染页面代码如下

{% extends 'base.html' %}

{% block title %}分类归档页面{% endblock %}

{% block main %}

    <div id="main">
        <div class="archives">

            {% for post in postobj %}
                <article class="archive-article archive-type-post">
                  <div class="archive-article-inner">
                    <header class="archive-article-header">
                    <a href="#" class="archive-article-date">
                         <time>{{ post.publish_time|date:'Y-m' }}</time>
                    </a>
                     <h1 itemprop="name">
                          <a class="archive-article-title" target="_blank" href="/post/{{ post.category_id }}">{{ post.title }}</a>
                     </h1>
                     </header>
                  </div>
                </article>
            {% empty %}
                无记录
            {% endfor %}

        </div>
    </div>

{% endblock %}

iii数据库截图如下:

blob.png

Python 全系列/第九阶段:Python_Django2 框架(隐藏)/Django项目阶段-博客项目 37473楼
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 37480楼
Python 全系列/第三阶段:Python 网络与并发编程/网络通信 37481楼
JAVA 全系列/(旧的隐藏)第十一阶段:spring全家桶(Spring Boot)/Spring Boot 37483楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637