Django. На странице не отображаются данные из таблицы
в проекте на джанго (сайт) имеется 2 приложения: home - основные все страницы сайта (index, about, contacts) blog - собственно блог
приложение блог собирал аналогично первому приложению.
Проблема: на главной странице блога, где будут выводиться все статьи/заметки/и тд... не цепляется таблица, соответственно данные не отображаются (в шаблоне хтмл использую тег {% for ... %}... {% empty %}. выводится то что в емпти теге указанно
blog.urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from .views import *
urlpatterns = [
path('', BlogMain.as_view(), name='blog'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
blog.views.py
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Posts, PostComments
# Create your views here.
class BlogMain(ListView):
model = Posts
template_name = 'blog/index_blog.html'
context_object_name = 'blog_posts'
paginate_by = 3
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)
return context
blog.models.py
from django.db import models
from django.urls import reverse
# Create your models here.
class Posts(models.Model):
title = models.CharField(max_length=150)
created_at = models.DateTimeField(auto_now_add=True)
category = models.CharField(max_length=100)
# tags =
description = models.TextField()
photo = models.ImageField(upload_to='media/%Y/%m/%d/', blank=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog', kwargs={"pk": self.pk})
class PostComments(models.Model):
title = models.CharField(max_length=150)
# post_id = models.ForeignKey(Posts, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now=True)
description = models.TextField()
def __str__(self):
return self.title
index_blog.html
{% extends 'base.html' %}
{% block title %}
Tbc Blog
{% endblock %}
<!-- main section -->
{% block main_section %}
<section class="blog-content">
{% for post in blog_posts %}
<div class="container container-padding">
<!-- blog item -->
<article class="blog-item">
<!-- header -->
<header>
<!-- title -->
<h2 class="title"><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>
<!-- meta -->
<ul class="meta list-inline">
<li class="list-inline-item">{{ post.created_at }}</li>
<li class="list-inline-item"><a href="#">{{ post.category }}</a></li>
<li class="list-inline-item"><a href="#">Tag's</a></li>
</ul>
</header>
<!-- thumb -->
<img src="{{ post.photo.url }}" alt="blog-thumb">
<!-- footer -->
<footer>
<!-- except -->
<p class="except">{{ post.description }}</p>
<!-- button -->
<a href="{{ post.get_absolute_url }}" class="btn btn-default">Read more</a>
</footer>
</article>
{% empty %}
<p>Sorry</p>
{% endfor %}
</div>
<div>
<nav class="pagination-outer">
<ul class="pagination justify-content-center">
<li class="page-item disabled"><a class="page-link" href="#">1</a></li>
<li class="page-item"><a class="page-link" href="#">2</a></li>
<li class="page-item"><a class="page-link" href="#">3</a></li>
</ul>
</nav>
</div>
</section>
{% endblock %}
Регистрация моделей в админке
:
admin.site.register(Posts)
admin.site.register(PostComments)
Главный файл urls проекта:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('home.urls')),
path('about/', about, name='about'),
path('contact/', contact, name='contact'),
path('blog/', include('blog.urls')),
]