Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django帖子未显示在主页上,但显示在类别列表中_Python_Django_Twitter Bootstrap - Fatal编程技术网

Python Django帖子未显示在主页上,但显示在类别列表中

Python Django帖子未显示在主页上,但显示在类别列表中,python,django,twitter-bootstrap,Python,Django,Twitter Bootstrap,我和Django写了一个博客。博客允许管理员创建帖子和类别。我使用bootstrap来对齐相邻的帖子。 这是我博客的主页。当柱子到达屏幕的另一端时。请看下面的示例: 邮政1邮政2邮政3邮政4邮政5邮政6邮政7邮政8邮政9邮政10邮政11邮政12邮政13邮政14邮政16。 post 16不会显示在主页上,但如果您转到其类别,它将显示在类别列表上,但不会显示在主页index.html上 index.html {% extends 'base.html' %} {% block content %

我和Django写了一个博客。博客允许管理员创建帖子和类别。我使用bootstrap来对齐相邻的帖子。 这是我博客的主页。当柱子到达屏幕的另一端时。请看下面的示例:

邮政1邮政2邮政3邮政4邮政5邮政6邮政7邮政8邮政9邮政10邮政11邮政12邮政13邮政14邮政16。


post 16
不会显示在主页上,但如果您转到其类别,它将显示在类别列表上,但不会显示在主页index.html上

index.html

{% extends 'base.html' %}

{% block content %}
{% if categories %}
<div class="tab">
{% for category in categories %}
<button class="tablinks"><a href="{{ category.get_absolute_url }}">{{ 
category.title }}</a></button>
{% endfor %}
{% else %}
<p>There are no posts.</p>
{% endif %}

</div>
<br><br><br>
<div class="container ">
{% if posts %}
<div class="row ">
{% for post in posts %}
 <div class="poosts col-md-2">
 <p class="past"><a class="link" href="{{ post.get_absolute_url }}"><span 
class="tda"> {{post.title}}</span><br><br><span class="postbody">
{{post.body|truncatewords:13}}</a></span></p>
    </div>
    {% endfor %} 

{% else %}
    <p class="text-center">There are no posts.</p>
</div>
{% endif %}


</div>
{% endblock %}
型号.py

from django.shortcuts import render, redirect, get_list_or_404
from django.http import  HttpResponse
from .models import Blog, Category
from django.shortcuts import render_to_response, get_object_or_404


def index(request):
     return render_to_response('account/index.html', {
        'categories': Category.objects.all(),
        'posts': Blog.objects.all()[:5]
     })

def view_post(request, slug):

    return render_to_response('account/view_post.html', {
           'post': get_object_or_404(Blog, slug=slug)
    })

def view_category(request, slug):
    category = get_object_or_404(Category, slug=slug)
    return render_to_response('account/view_category.html', {
           'category': category,
           'posts': Blog.objects.filter(category=category)[:5]
    })
from django.db import models
from django.db.models import permalink

class Blog(models.Model):
      title = models.CharField(max_length=100, unique=True)
      slug = models.SlugField(max_length=100, unique=True)
      body = models.TextField()
      posted = models.DateTimeField(db_index=True, auto_now_add=True)
      category = models.ForeignKey('Category')

def __unicode__(self):
    return '%s' % self.title

@permalink
def get_absolute_url(self):
    return ('view_blog_post', None, { 'slug': self.slug })


def __str__(self):
    return self.title

class Category(models.Model):
      title = models.CharField(max_length=100, db_index=True)
      slug = models.SlugField(max_length=100, db_index=True)

def __unicode__(self):
    return '%s' % self.title


@permalink
def get_absolute_url(self):
    return ('view_blog_category', None, { 'slug': self.slug })

def __str__(self):
    return self.title

您应该尝试在views.py中打印
Category.objects.all()
,然后在索引中打印
Category.objects.all()
,并用输出进行响应。在主页上,它只显示5篇文章。您在views.py中的意思是什么output@Nawaf您使用
Blog.objects.all()[:5]
将其限制为5篇文章。
from django.db import models
from django.db.models import permalink

class Blog(models.Model):
      title = models.CharField(max_length=100, unique=True)
      slug = models.SlugField(max_length=100, unique=True)
      body = models.TextField()
      posted = models.DateTimeField(db_index=True, auto_now_add=True)
      category = models.ForeignKey('Category')

def __unicode__(self):
    return '%s' % self.title

@permalink
def get_absolute_url(self):
    return ('view_blog_post', None, { 'slug': self.slug })


def __str__(self):
    return self.title

class Category(models.Model):
      title = models.CharField(max_length=100, db_index=True)
      slug = models.SlugField(max_length=100, db_index=True)

def __unicode__(self):
    return '%s' % self.title


@permalink
def get_absolute_url(self):
    return ('view_blog_category', None, { 'slug': self.slug })

def __str__(self):
    return self.title