简体   繁体   中英

Django model can't be render in html

There are some models that I've created but it wont render in template, there are some that are ok so i just use the same method to create the other model but when i try manage.py runserver some of the model is not render, i already do manage.py makemigrations and manage.py migrate, sorry this is my first time asking question here and thank you

This is my views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import FrontPage, DailyQuote, Photo, kate


# Create your views here.

def front(request):
    photo = Photo.objects.all()
    content = {'photo': photo}
    return render(request, 'front.html', content)


def home(request):
    page = FrontPage.objects.all()
    args = {'page':page}
    return render(request, 'Home.html', args)

def ytvideos(request):
    return render(request, 'ytvideos.html')

def reading(request):
    page = FrontPage.objects.all()
    Fullpage = {'page': page}
    return render(request, 'Reading.html', Fullpage)

def KGK(request):
    Thekate = kate.objects.all()
    Therealkate = {'Thekate': Thekate}
    return render(request, 'KGK.html', Therealkate)

This is my HTML

{% extends 'front.html' %}

{% block head %}



{% endblock %}

{% block content %}

  <div class="container-fluid">
      {% for i in kate %}
      <div class="card text-center">
        <div class="card-header">
            <b>kate</b>
        </div>
        <div class="card-body">
            <h5 class="card-title">{{ i.katetitle }}</h5>
            <p class="card-text">{{ i.katecontent }}</p>
            <a href="#" class="btn btn-primary">Read More</a>
        </div>
      </div>
      {% endfor %}
  </div>





{% endblock %}

This is my admin.py

from django.contrib import admin
from .models import FrontPage, DailyQuote, Photo, kate

# Register your models here.
admin.site.register(FrontPage)
admin.site.register(DailyQuote)
admin.site.register(Photo)
admin.site.register(kate)

This is my models.py

from django.db import models


# Create your models here.

class FrontPage(models.Model):
    Title = models.CharField('Title', max_length=20)
    Content = models.TextField('Content', max_length=100)
    short = models.TextField('short', max_length=150)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.Title

    class Meta:
        ordering = ['-date']


class DailyQuote(models.Model):
    Quote = models.TextField('Quote', max_length=200)
    Author = models.CharField('Author', max_length=50)

    def __str__(self):
        return self.Quote


class Photo(models.Model):
    Picture = models.ImageField('Image', upload_to="images/")
    Caption = models.CharField('Caption', max_length=20)
    text = models.TextField('text', max_length=50)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.text

    class Meta:
        ordering = ['-date']

class kate(models.Model):
    katetitle = models.CharField('Title', max_length=20)
    katecontent = models.TextField('Content', max_length=100)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.katecontent

    class Meta:
        ordering = ['-date']

My urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('front', views.front, name='front'),
    path('ytvideos', views.ytvideos, name='ytvideos'),
    path('Reading', views.reading, name='reading'),
    path('KGK', views.KGK, name='KGK'),
]

The dictionary key is 'Thekate' in Therealkate = {'Thekate': Thekate}

It's trying to find something called 'kate', which is actually supposed to be called 'Thekate'

This should fix it-

  <div class="container-fluid">
  {% for i in Thekate %}
  <div class="card text-center">
    <div class="card-header">
        <b>kate</b>
    </div>
    <div class="card-body">
        <h5 class="card-title">{{ i.katetitle }}</h5>
        <p class="card-text">{{ i.katecontent }}</p>
        <a href="#" class="btn btn-primary">Read More</a>
    </div>
  </div>
  {% endfor %}

In the for loop, I just changed 'kate' to 'Thekate'

Hope it works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM