gpt4 book ai didi

python - django 模板帮助,颠倒

转载 作者:行者123 更新时间:2023-11-28 05:22:51 25 4
gpt4 key购买 nike

我正在尝试使用 Django 1.8 构建我的博客,但是我不知道如何订购这些博客。看图enter image description here

我想在底部显示“最早”,在顶部显示“最新”。这是我的

index.html

{% extends 'layouts/base.html' %}
{% block title %}
Homepage - {{ block.super }}
{% endblock title %}
{% block content %}
<center>
{% for blog in blogs %}
<h2><a href="{% url 'blog_detail' slug=blog.slug %}">
{{ blog.name }}
</a></h2>
<p>{{ blog.description }}</p>
{% endfor %}
</center>
{% endblock content %}

模型.py

# -*- coding: utf-8 -*-
from django.db import models
from django.utils import timezone


class blog(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
slug = models.SlugField(unique=True)
date_time = models.DateTimeField(auto_now_add = True)

def __unicode__(self):
return self.name



def get_image_path(instance, filename):
return '/'.join(['blog_images', instance.bupimg.slug, filename])

class Upload(models.Model):
bupimg = models.ForeignKey(blog, related_name="uploads")
image = models.ImageField(upload_to=get_image_path)

views.py

from django.shortcuts import render
from blogging.models import *


def index(request):
blogs = blog.objects.all()
return render(request, 'index.html', {
'blogs':blogs,
})

def blog_detail(request, slug):
article = blog.objects.get(slug=slug)
uploads = article.uploads.all()
return render(request, 'blogs/blog_detail.html', {
'article': article,
'uploads': uploads,
})

如何将博客标题“最早”移至下方,将“最新”移至顶部?我需要让最新的博客显示在顶部。

最佳答案

您没有对博客进行排序,它们是随机排列的。换行试试

blogs = blog.objects.all()

blogs = blog.objects.order_by('-date_time')

减号(-)表示降序,即。从最新到最旧。

关于python - django 模板帮助,颠倒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36111657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com