Python Web开发:通知公告

来自CloudWiki
跳转至: 导航搜索

实训步骤

修改视图函数

homeApp/views.py:

def home(request):
    ...
    # 通知公告
    noteList = MyNew.objects.all().filter(
        Q(newType='通知公告')).order_by('-publishDate')
    if (len(noteList) > 4):
        noteList = noteList[0:4]


   ...
   # 返回结果
   return render(request, 'home.html', {
        'active_menu': 'home',
        'postList': postList,
        'newList': newList,
        'noteList': noteList,
    })

编辑前端页面

<!-- 通知公告 -->
      <span class="part1">
        <a href="#">通知公告</a>
      </span>
      <span class="part1 en">
          / Public Release
      </span>
      <a class="btn btn-default btn-xs more-btn" href="{% url 'newsApp:news' 'notice' %}">
        + 更多
      </a>
      <div class="line1">
        <div class="line2 theme"></div>
      </div>
      <div>
        <img class="img-responsive" src="{% static 'img/note.jpg' %}">
        <ul class="list-unstyled list-new">
          {% for note in noteList %}
          <li>
            <a href="{% url 'newsApp:newDetail' note.id %}">
              {{note.title|truncatechars:"25"}}
            </a>
            <a href="{% url 'newsApp:newDetail' note.id %}" class="public-detail">
              查看详情>>
            </a>
          </li>
          {% endfor %}
        </ul>
      </div>

效果

Python21060801.png