3
votes

I made a blog with Django. The blog lets the admin create posts and categories. I used bootstrap to align the posts next to each other. This is the homepage of my blog. When the posts reach the other end of the screen. look at the Example below:

post1 post2 post3 post4 post5 post6 post7 post8 post9 post10 post11 post12 post 13 post14 post16.


post 16 wouldnot show on the homepage, but if you go to its category it will show on the category list but not on the homepage which is 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 %}

categories page

{% extends 'base.html' %}
{% block head_title %}Viewing category {{ category.title }}{% endblock %}
{% block title %}{{ category.title }}{% endblock %}

{% block content %}
<br><br><br>
<div class="container">
{% if posts %}
<div class="row">
{% for post in posts %}
<div class="poosts col-md-4">

<p class="past"><a class="link" href="{{ post.get_absolute_url }}"><span 
class="tda"> {{post.title}}</span><br><br>{{post.body|truncatewords:13}}</a>
</p>
</div>
{% endfor %} 

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

</div>
{% endblock %}

views.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]
    })

models.py

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
1
try printing Category.objects.all() in index and respond with outputVasif
On the homepage it only shows 5 postsuser8168373
do you mean in the views.py and can what to you mean respond with outputuser8168373
@Nawaf You are limiting it to 5 posts with Blog.objects.all()[:5].Paulo Almeida

1 Answers

0
votes

you should try this 'posts': Blog.objects.all() in views.py