{% extends "base.html" %}
{% load static %}
{% block title %}Home{% endblock %}
{% block content %}
<div class="w-9/12 px-14 flex justify-between mt-60 h-full">
<div class="card-container grid grid-cols-2 gap-8 m-8">
{% for posts in posts %}
<div class="card flex justify-between shadow-md">
<div class="p-5 basis-1/2">
<h2 class="text-lg font-medium">{{ posts.title }}</h2>
<div class="flex flex-col">
<span class="text-gray-400">{{ posts.writer }}</span>
<span class="text-gray-400">{{ posts.slug }}</span>
<span class="text-gray-400"> {{ posts.created_on }}</span>
</div>
<p class="text-sm pt-2 text-slate-700">{{ posts.content }}</p>
<button class="bg-cyan-500 text-white px-3 py-2 rounded-md hover:bg-cyan-700 transition my-6">Read More</button>
</div>
<div class="card-image basis-1/2">
<img src="{{ object.posts.image.url }}" >
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
models.py
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import User
# Create your models here.
STATUS = ((0, "Draft"), (1, "Published"))
class Gender(models.TextChoices):
MALE="M",_("Male")
FEMALE="F",_("Female")
class Content(models.Model):
name=models.CharField(max_length=100)
def __str__(self) -> str:
return self.name
class Author(models.Model):
name=models.CharField(max_length=100)
def __str__(self) -> str:
return self.name
class Profile(models.Model):
firstname=models.CharField(max_length=100)
lastname=models.CharField(max_length=100)
gender=models.CharField(max_length=1,choices=Gender)
mobile=models.CharField(max_length=20)
email=models.EmailField(max_length=100)
course=models.CharField(max_length=100)
def __str__(self) -> str:
return f'{self.firstname} {self.lastname}'
class BlogProfile(models.Model):
content=models.ForeignKey(Content,on_delete=models.CASCADE)
author=models.ForeignKey(Author,on_delete=models.CASCADE)
post=models.TextField(blank=True)
def __str__(self) -> str:
return f'{self.content.name} -> {self.author.name}'
class Post(models.Model):
title=models.CharField(max_length=200, unique=True)
slug=models.SlugField(max_length=200, unique=True)
image=models.ImageField(upload_to='images/',null=True,blank=True)
writer=models.ForeignKey(User, on_delete=models.CASCADE)
created_on=models.DateTimeField(auto_now_add=True)
updated_on=models.DateTimeField(auto_now=True)
content=models.TextField()
status=models.IntegerField(choices=STATUS, default=0)
def __str__(self):
return self.title
views.py
from django.views.generic.base import TemplateView
from django.shortcuts import render
from .models import Post
# from .models import BlogProfile
def HomePageView(request):
context = {
'posts': Post.objects.all()
}
return render(request, 'home.html', context)
Welcome @DoTMInDs !
Please provide the full error message along with the traceback being received.
In the absence of seeing the complete error, in the context in which it was generated, I do see one thing that doesn’t seem right.
Your context has one entry, 'posts'
:
You are not supplying an entry in your context named object
:
Side note: To avoid confusion, I strongly recommend against using the same name for the instance in a for
loop as the context variable name:
I would recommend this be changed to for post in posts
, with the corresponding changes to the rest of that portion of the template.
oh okay, let me try that
please can you further explain this
Looking at your template, you have:
What is this variable (object.posts.image.url
) a reference to?