Data dari database ke html tidak dirender. Data statis sedang dirender tetapi sayangnya data basis data tidak dirender tidak ada kesalahan yang datang dan halaman juga dirender tetapi data tidak dirender dari database ke halaman

Html dokumen

{% 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)

Ini adalah file model py 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
1
Max_len 1 Oktober 2021, 13:35

2 jawaban

Jawaban Terbaik

Coba ini:

{% 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 %}
0
joao_lucas 1 Oktober 2021, 10:55

Seharusnya {% for post in posts %}

0
Ene P 1 Oktober 2021, 10:37