Reputation: 11
im trying to follow this django tutorial, there a three section on my website called home blog and content.
what should happen, is i click on blog and it list out the blog post i made in the admin and i can click on the post and read what the blog post says, but when i click on blogs nothing shows up, its just blank.
i have already installed apps in settings
i added the url in the url.py folder
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('personal.urls')),
url(r'^blog/', include('blog.urls')),
I went into the models folder and created a model
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=140)
body = models.TextField()
date = models.DateTimeField()
def __str__(self):
return self.title
in my blogs/urls folder i have
from django.conf.urls import url, include
from django.views.generic import ListView, DetailView
from blog.models import Post
from django.urls import path
urlpatterns = [ path('', ListView.as_view(queryset=Post.objects.all().order_by("-date")[:25],
template_name="blog/blog.html")),
path('<int:pk>/', DetailView.as_view(model=Post,
template_name='blog/post.html'))]
in my blog folder i made a new directory called templates and in templates i made another directory called blog and in the folder i made an html file called blog.html and it is in a notepad++. this is what is in that blog.html notepad
{% extends "personal/header.html" %}
{% block content %}
{% for post in obejects_list %}
<h5>{{ post.date|date:"Y-m-d" }}<a href=
"/blog/{{post.id}}"> {{post.title}}</a></h5>
{% endfor %}
{% endblock %}
and i went into admin folder and added
from django.contrib import admin
from blog.models import Post
admin.site.register(Post)
Upvotes: 0
Views: 500
Reputation: 68
try to add context_object_name to your ListView class like this :
path('', ListView.as_view(queryset=Post.objects.all().order_by("-date")[:25],
template_name="blog/blog.html", context_object_name = "posts")),
then in your template do like this :
{% extends "personal/header.html" %}
{% block content %}
{% for post in posts %}
<h5>{{ post.date|date:"Y-m-d" }}<a href=
"/blog/{{post.id}}"> {{post.title}}</a></h5>
{% endfor %}
{% endblock %}
Upvotes: 0