Home > Back-end >  Data not rendering from django database to html
Data not rendering from django database to html

Time:10-02

Data from database to html is not rendering. The static data is rendering but unfortunately the database data is not rendering no error is comming and page is alo rendering but data is not rendering from databse to the page

Html doc

{% load static %}

{% block title %}
All Tractors
{% endblock  %}

{% block content%}
<section id="all_events">
    <h1>All Tractors</h1>
    <h1>{{posts.implimentaion}} hi</h1>
    <br>
    <ul>
        {% for posts in post %}
            {% include "tractor/includes/singlePost.html" %}
        {% endfor %}
    </ul>

</section>
{% endblock %}

Vews.py

from django import forms
from django.contrib.auth.models import User
from django.shortcuts import render, redirect
from .models import Post, Farmer
# Create your views here.
from django.http import HttpResponseRedirect
# Create your views here.
def starting_page(request):
    return render(request,"tractor/index.html")

def posts(request):
    qs = Post.objects.all()  
    context = {"posts":qs}
    return render(request,"tractor/all-post.html",context)

This is the models py file Models.py

from django.core.validators import MinLengthValidator
from django.db.models.signals import pre_save
# Create your models here.
from .utils import unique_slug_generator

class Farmer(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    phone =models.CharField(max_length=10)
    address = models.TextField(max_length=200)
    email_address = models.EmailField()

    def full_name(self):
        return f"{self.first_name} {self.last_name}"

    def __str__(self):
        return self.full_name()


class Post(models.Model):
    tractor_price = models.IntegerField(max_length=150)
    implimentaion = models.CharField(max_length=50)
    purchase_date = models.DateField(auto_now=False)
    slug = models.SlugField(unique=True, db_index=True, null=True,blank=True)
    tractor_company = models.CharField(max_length=50)
    tractor_model = models.CharField(max_length=50)
    description = models.TextField(validators=[MinLengthValidator(10)])
    date = models.DateField(auto_now=False)
    farmer = models.ForeignKey(
        Farmer, on_delete=models.SET_NULL, null=True, related_name="posts")

    def __str__(self):
        return self.implimentaion

CodePudding user response:

It should be {% for post in posts %}

CodePudding user response:

Try this:

{% load static %}

{% block title %}
All Tractors
{% endblock  %}

{% block content%}
<section id="all_events">
    <h1>All Tractors</h1>
    <ul>
      {% for post in posts %}
        <h1>{{post.implimentaion}} hi</h1>
        <br>
        {% include "tractor/includes/singlePost.html" %}
      {% endfor %}
    </ul>

</section>
{% endblock %}
  • Related