HTML中的Django循环没有按顺序进行

HTML中的Django循环没有按顺序进行,html,django,loops,Html,Django,Loops,我的HTML模板中有以下代码块: <main role="main"> <div class="container"> <h1 class="text-center pt-5">welcome to my blogs</h1> <br> <br> <h2>my latest blog</h2> <hr/>

我的HTML模板中有以下代码块:

<main role="main">
    <div class="container">
        <h1 class="text-center pt-5">welcome to my blogs</h1>
        <br>
        <br>
        <h2>my latest blog</h2>
        <hr/>
        {% for e in allblogs.all %}
        <a href="{% url 'blog_detail' e.id %}"><h1>{{ e.title }}</h1></a>
        <h6>{{ e.pretty_time }}</h6>
        <img class="img-fluid" height="400" width="300" src="{{ e.image.url }}">
            <p>{{ e.summary }}</p>
        {% endfor %}
    </div>
</main>
<main role="main">
    <div class="container">
        <h1 class="text-center pt-5">welcome to my blogs</h1>
        <br>
        <br>
        <h2>my latest blog</h2>
        <hr/>
        {% for e in allblogs.all %}
        <a href="{% url 'blog_detail' e.id %}"><h1>{{ e.title }}</h1></a>
        <h6>{{ e.pretty_time }}</h6>
        <img class="img-fluid" height="400" width="300" src="{{ e.image.url }}">
            <p>{{ e.summary }}</p>
        {% endfor %}
    </div>
</main>
views.py

from django.shortcuts import render, get_object_or_404
from .models import Blog

# Create your views here.
def allblogs(request):
    allblogs = Blog.objects
    return render(request, 'blog/allblogs.html', {'allblogs':allblogs})

def blog_detail(request, blog_id):
    blogdetail=get_object_or_404(Blog, pk=blog_id)
    return render(request, 'blog/blog_detail.html', {'blog': blogdetail})
from django.db import models
from datetime import datetime

# Create your models here.
class Blog(models.Model):
    title = models.CharField(max_length=300, blank=True)
    image = models.ImageField(upload_to="image/")
    text_body = models.TextField(blank=True)
    pub_date = models.DateTimeField(default=datetime.now, blank=True)

    def __str__(self):
        return self.title

    def summary(self):
        return self.text_body[:100]

    def pretty_time(self):
        return self.pub_date.strftime('%b %e %Y')
models.py

from django.shortcuts import render, get_object_or_404
from .models import Blog

# Create your views here.
def allblogs(request):
    allblogs = Blog.objects
    return render(request, 'blog/allblogs.html', {'allblogs':allblogs})

def blog_detail(request, blog_id):
    blogdetail=get_object_or_404(Blog, pk=blog_id)
    return render(request, 'blog/blog_detail.html', {'blog': blogdetail})
from django.db import models
from datetime import datetime

# Create your models here.
class Blog(models.Model):
    title = models.CharField(max_length=300, blank=True)
    image = models.ImageField(upload_to="image/")
    text_body = models.TextField(blank=True)
    pub_date = models.DateTimeField(default=datetime.now, blank=True)

    def __str__(self):
        return self.title

    def summary(self):
        return self.text_body[:100]

    def pretty_time(self):
        return self.pub_date.strftime('%b %e %Y')

我首先从下面的代码中获得了输出
/blog/2
,然后是
/blog/1
。有了这些代码,在我的循环中,
blog/2
之前不应该输出
blog/1

HTML模板:

<main role="main">
    <div class="container">
        <h1 class="text-center pt-5">welcome to my blogs</h1>
        <br>
        <br>
        <h2>my latest blog</h2>
        <hr/>
        {% for e in allblogs.all %}
        <a href="{% url 'blog_detail' e.id %}"><h1>{{ e.title }}</h1></a>
        <h6>{{ e.pretty_time }}</h6>
        <img class="img-fluid" height="400" width="300" src="{{ e.image.url }}">
            <p>{{ e.summary }}</p>
        {% endfor %}
    </div>
</main>
<main role="main">
    <div class="container">
        <h1 class="text-center pt-5">welcome to my blogs</h1>
        <br>
        <br>
        <h2>my latest blog</h2>
        <hr/>
        {% for e in allblogs.all %}
        <a href="{% url 'blog_detail' e.id %}"><h1>{{ e.title }}</h1></a>
        <h6>{{ e.pretty_time }}</h6>
        <img class="img-fluid" height="400" width="300" src="{{ e.image.url }}">
            <p>{{ e.summary }}</p>
        {% endfor %}
    </div>
</main>
型号:

from django.db import models
from datetime import datetime

# Create your models here.
class Blog(models.Model):
    title = models.CharField(max_length=300, blank=True)
    image = models.ImageField(upload_to="image/")
    text_body = models.TextField(blank=True)
    pub_date = models.DateTimeField(default=datetime.now, blank=True)

    def __str__(self):
        return self.title

    def summary(self):
        return self.text_body[:100]

    def pretty_time(self):
        return self.pub_date.strftime('%b %e %Y')
输出:

<a href="/blog/2/"><h1>second blog</h1></a>
        <h6>Jun 26 2018</h6>
        <img class="img-fluid" height="400" width="300" src="/media/image/ben-tatlow-635718-unsplash.jpg">
            <p>second blog with new picturefirst blog pictures in something is fun to learn, but it takes lots of t</p>

        <a href="/blog/1/"><h1>first blog</h1></a>
        <h6>Jun 26 2018</h6>
        <img class="img-fluid" height="400" width="300" src="/media/image/aaron-burden-649463-unsplash.jpg">
            <p>first blog pictures in something is fun to learn, but it takes lots of time, at same time I would ha</p>

        <a href="/blog/3/"><h1>third blog</h1></a>
        <h6>Jun 26 2018</h6>
        <img class="img-fluid" height="400" width="300" src="/media/image/caleb-lucas-434609-unsplash.jpg">
            <p>even more pictures first blog pictures in something is fun to learn, but it takes lots of time, at s</p>
<a href="/blog/2/"><h1>second blog</h1></a>
        <h6>Jun 26 2018</h6>
        <img class="img-fluid" height="400" width="300" src="/media/image/ben-tatlow-635718-unsplash.jpg">
            <p>second blog with new picturefirst blog pictures in something is fun to learn, but it takes lots of t</p>

        <a href="/blog/1/"><h1>first blog</h1></a>
        <h6>Jun 26 2018</h6>
        <img class="img-fluid" height="400" width="300" src="/media/image/aaron-burden-649463-unsplash.jpg">
            <p>first blog pictures in something is fun to learn, but it takes lots of time, at same time I would ha</p>

        <a href="/blog/3/"><h1>third blog</h1></a>
        <h6>Jun 26 2018</h6>
        <img class="img-fluid" height="400" width="300" src="/media/image/caleb-lucas-434609-unsplash.jpg">
            <p>even more pictures first blog pictures in something is fun to learn, but it takes lots of time, at s</p>

2018年6月26日
第二个有新图片的博客第一个有图片的博客学习起来很有趣,但要花很多时间

2018年6月26日 第一个博客图片中的东西是有趣的学习,但它需要很多时间,同时我会哈

2018年6月26日 甚至更多的图片第一个博客图片在某物是有趣的学习,但它需要很多时间,在s


您应该在视图中使用order\u by方法

#views.py
def allblogs(request):
    allblogs = Blog.objects.all().order_by("id")
    return render(request, 'blog/allblogs.html', {'allblogs':allblogs})

您用于此模板的视图是什么?我认为问题在于您在模板中编写了视图中应该包含的逻辑:
order\u by
。请共享(相关)模型和视图。从django.shortcuts导入渲染,从获取对象或404。模型导入博客#在此处创建视图。def allblogs(request):allblogs=Blog.objects返回render(request,'Blog/allblogs.html',{'allblogs':allblogs})def Blog_detail(request,Blog_id):blogdeail=get_object_或_404(Blog,pk=Blog_id)返回render(request,'Blog/Blog_detail.html',{'Blog':blogdeal})请从答案部分删除您的问题,试着回答“UtkucanBıyıklı”。快乐编码:)