Home > database >  Block content not rendering
Block content not rendering

Time:06-12

I want to retrieve all posts from my Post model on one view template(allposts.html), and all posts from the politics category on another view template(political.html), and then display them on different sections of the index.html template. However, I am getting a blank page. What could be the issue? Here is my code

index.html

 <!-- ======= Post Grid Section ======= -->
    
    <section id="posts" >
      <div  data-aos="fade-up">
        <div >
          {% block allposts %}
          {% endblock %}
              <!-- Some sections skipped -->
<div>
              <div ><span >Culture</span> <span >&bullet;</span> <span>Jul 5th '22</span></div>
              <h3><a href="single-post.html">What is the son of Football Coach John Gruden, Deuce Gruden doing Now?</a></h3>
              <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Distinctio placeat exercitationem magni voluptates dolore. Tenetur fugiat voluptates quas, nobis error deserunt aliquam temporibus sapiente, laudantium dolorum itaque libero eos deleniti?</p>
              <div >
                <div ><img src="assets/img/person-2.jpg" alt="" ></div>
                <div >
                  <h3 >Wade Warren</h3>
                </div>
              </div>
            </div>
          </div>
          {% block content %}
          {% endblock %}

Here is the political.html content that I am trying to render

{% extends 'index.html' %}
{% block content %}
    {% for politics in politics %}
    <div >
        <div >
        <a href="single-post.html"><img src="{{post.image.url}}" alt="" ></a>
        <div ><span >{{post.category}}</span> <span >&bullet;</span> <span>{{post.created_at}}</span></div>
        <h2 ><a href="single-post.html">{{post.title}}</a></h2>
        <span >Ole Pundit</span>
        <p >{{post.body|truncatewords:75}}</p>
        </div>
    </div>
    {% endfor %}
{% endblock %}

And allposts.html

{% extends 'index.html' %}
{% block allposts %}
{% for post in posts %}
  <div >
    <a href="single-post.html"><img src="{{post.image.url}}" ></a>
      <div>
        <div ><span >{{post.category}}</span> <span >&bullet;</span> <span>{{post.created_at}}</span></div>
        <h2><a href="">{{post.title}}</a></h2>
      </div>
    <p >{{post.body|truncatewords:75}}</p>

    <div >
      <div ><img src="{% static 'assets/img/person-1.jpg' %}" alt="" ></div>
      <div >
        <h3 >OlePundit</h3>
      </div>
    </div>
  </div>
{% endfor %}
{% endblock %}

Here is my models.py

from django.db import models
from datetime import datetime
from django_resized import ResizedImageField

# Create your models here.
class Post(models.Model):
    title = models.CharField(max_length=100)
    body = models.CharField(max_length=1000000)
    created_at = models.DateTimeField(default=datetime.now, blank = True)
    image =  ResizedImageField(size=[250, 200], upload_to='img')
    category = models.CharField(max_length=100)

And my urls

from django.urls import path
from . import views
from django.conf.urls.static import static
from django.conf import settings
from django.contrib import admin

urlpatterns= [
    path('', views.index, name='index'),
    path('<category>/<str:pk>', views.post, name='post'),
    path('political', views.political, name='political'),
    path('allposts', views.allposts, name='allposts')
]

if settings.DEBUG:
     urlpatterns  = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Views.py

from django.shortcuts import render
from .models import Post
from django.views.generic import DetailView

# Create your views here.

def index(request):
    return render(request, 'blog/index.html')

def allposts(request):
    posts = Post.objects.all()
    return render(request, 'blog/allposts.html', {'posts':posts})

def political(request):
    politics = Post.objects.filter(category__contains='politics')
    return render(request, 'blog/political.html', {'politics':politics})

def post(request, pk):
    posts = Post.objects.get(id=pk)
    return render(request, 'blog/posts.html', {'posts':posts})  

CodePudding user response:

Have you tried this solution?

In the political.html file, in the beginning, simply add

 {% block allposts %}
  {% endblock %}

And in the allposts.html file, add this syntax at the end of your file:

  {% block content %}
  {% endblock %}

CodePudding user response:

In your political.html. your forloop starts

{% for politics in politics%}

But then inside the forloop you use {{post.image}} {{post.title}} ect..

All of those should be politics not post. It looks like you copy and pasted from allposts.html, created the correct start of the forloop with politics, but didn't change the rest of the forloop: {{politics.image.url}} {{politics.title}} ect..

  • Related