i created a blog
this is my models.py configurations:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
# Create your models here.
class PublishedManager(models.Manager):
def get_queryset(self):
return super(PublishedManager, self).get_queryset()\
.filter(status='published')
class post(models.Model):
STATUS_CHOICE=(
('draft','DRAFT'),
('published','Published'),
('admin','admin'),
)
title=models.CharField(max_length=250,null=True)
author=models.ForeignKey(User,related_name='blog_posts',null=True)
slug=models.SlugField(max_length=250,unique_for_date='publish',null=True)
body=models.TextField(default='')
publish=models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True,null=True)
updated =models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10,
choices =STATUS_CHOICE,
default='draft')
objects = models.Manager()
published = PublishedManager()
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:post_detail',args=[self.publish.year,
self.publish.strftime('%m'),
self.publish.strftime('%dz'),
self.slug])
and this is my views.py:
from django.shortcuts import render
from .models import post
# Create your views here.
def post_list(request):
posts= post.published.all()
return render(request,'blog/post/list.html',{posts:posts})
def post_detail(request,year,month,day,post):
post=get_object_or_404(post,slug=post,
status = 'published',
publish__year=year,
publish__month=month,
publish__day=day)
return render(request,'blog/post/index.html',{'post':post})
and this is my base.html:
{%load staticfiles%}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{% block title %}
<link rel="stylesheet" href={% static "/css/blog.css"%}>
{% endblock %}
</title>
</head>
<body>
<div id="content">
{% block content %}
{% endblock %}
</div>
<div id="sidebar">
<h2>my blog</h2>
<h3>this blog</h3>
</div>
</body>
</html>
and my list.html file:
{% extends "blog/base.html" %}
{% block title %}My blog{% endblock %}
{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
{{ post.title }}
</h2>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% endblock %}
and this is my urls.py (app folder)
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.post_list,name = 'post_list'),
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<post>[-\W]+)/$',
views.post_detail,
name='post_detail'),
]
and this is my urls.py file of my project folder:
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^blog/', include('myblog.urls',namespace='blog',app_name='blog')),
]
and i have added some post objects using admin user but when i tried to access the page the expected output according to the tutorial looks like this:
enter image description here
but instead my output was :
enter image description here
Firstly, the key in your context dictionary should be the string 'posts', not the variable posts, as #ABDUL pointed out in the comments.
return render(request,'blog/post/list.html',{'posts':posts})
Then you need to fix the NoReverseMatch error. In your get_absolute_url method, you have %dz. It should be '%d'. Remove the z.
def get_absolute_url(self):
return reverse('blog:post_detail',args=[self.publish.year,
self.publish.strftime('%m'),
self.publish.strftime('%d'),
self.slug])
Related
Thank you for taking the time to help! I've been stuck for hours. I'm learning django by going through this fantastic youtube video: https://www.youtube.com/watch?v=sm1mokevMWk&t=4252s. I believe I copied the code from the video exactly, and I double and triple checked it. Yet, despite declaring method = "post" in "create".html django consistently uses a get response. WHY?!
#urls.py
from django.urls import path
from . import views
urlpatterns = [
path('<int:id>', views.index, name='index'),
path("",views.home, name = 'home'),
path("create/", views.create, name="create"),
]
#views.py
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from .models import ToDoList, Item
from .forms import CreateNewList
def index(response, id):
ls = ToDoList.objects.get(id=id)
return render(response, 'main/list.html', {"ls":ls})
def home(response):
return render(response, "main/home.html", {})
def create(response):
print(response.method)
if response.method == "POST":
form = CreateNewList(response.POST)
if form.is_valid():
n = form.cleaned_data['name']
t = ToDoList(name=n)
t.save()
return HttpResponseRedirect("/%i" %t.id)
else:
form = CreateNewList()
return render(response, "main/create.html", {"form":form})
#create.html
{% extends 'main/base.html' %}
{% block title %} Create New List {% endblock %}
{% block content %}
Create Pages
<form method="post" action="/create/">
{{form.as_p}}
<button type="submit", name ="save" >Create New</button>
</form>
{% endblock %}
#base.html
<html>
<head>
<title>{% block title %}Jeff's website{% endblock %}</title>
</head>
<body>
<div id="content", name="content">
{% block content %}
{% endblock %}
</div>
</body>
</html>
#home.html
{% extends 'main/base.html' %}
{% block title %}
Home
{% endblock %}
{% block content %}
<h1>Home Page</h1>
{% endblock %}
#models.py
from django.db import models
class ToDoList(models.Model):
name = models.CharField(max_length=200)
def __str__(self):
return self.name
class Item(models.Model):
todolist = models.ForeignKey(ToDoList, on_delete=models.CASCADE)
text = models.CharField(max_length=300)
complete = models.BooleanField()
def __str__(self):
return self.text
You need to perform a GET request first to display your form and then , you make a post request for the submit, that why you check the request method .
if response.method == "POST" => The form is already displayed and we need to submit it
else we need to display our form
I'm new to Django and programming in general. I'm going through the book Django For Beginners and have added my own flair in the forms section but have run into a problem. Below I have detailed the error and provided my code.
ERROR
NoReverseMatch at /drug/12/
Reverse for 'drug_edit' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['drug/(?P[0-9]+)/edit/$']
Request Method: GET
Request URL: http://127.0.0.1:8000/drug/12/
Django Version: 3.1.3
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'drug_edit' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['drug/(?P[0-9]+)/edit/$']
Exception Location: C:\Users\scott\django_sites\history_cloud1\history1_env\lib\site-packages\django\urls\resolvers.py, line 685, in _reverse_with_prefix
Python Executable: C:\Users\scott\django_sites\history_cloud1\history1_env\Scripts\python.exe
Python Version: 3.8.3
models.py
from django.db import models
from django.urls import reverse
class DrugInfo(models.Model):
drug_name = models.CharField(max_length=75)
patient_name = models.CharField(max_length=25, default='Enter Patient Name',)
author = models.ForeignKey(
'auth.User',on_delete=models.CASCADE,
)
def __str__(self):
return self.drug_name
def get_absolute_url(self):
return reverse('drug_detail', args=[str(self.id)])
urls.py
from django.urls import path
from .views import (
drugListView,
drugDetailView,
drugCreateView,
drugDeleteView,
drugUpdateView,
)
urlpatterns = [
path('drug/<int:pk>/delete/',
drugDeleteView.as_view(), name='drug_delete'),
path('drug/new/', drugCreateView.as_view(), name='drug_new'),
path('drug/<int:pk>/', drugDetailView.as_view(), name='drug_detail'),
path('drug/<int:pk>/edit/',
drugUpdateView.as_view(), name='drug_edit'),
path('', drugListView.as_view(), name='home'),
]
views.py
from django.views.generic import ListView, DetailView
from django.views.generic.edit import (
CreateView, UpdateView, DeleteView
)
from django.urls import reverse_lazy
from .models import DrugInfo
class drugListView(ListView):
model = DrugInfo
template_name = 'home.html'
class drugDetailView(DetailView):
model = DrugInfo
template_name = 'drug_detail.html'
class drugCreateView(CreateView):
model = DrugInfo
template_name = 'drug_new.html'
fields = ['drug_name', 'patient_name', 'author']
class drugUpdateView(UpdateView): # new
model = DrugInfo
template_name = 'drug_edit.html'
fields = ['drug_name', 'patient_name']
class drugDeleteView(DeleteView): # new
model = DrugInfo
template_name = 'drug_delete.html'
success_url = reverse_lazy('home')
drug_edit.html
{% extends 'base.html' %}
{% block content %}
<h1>Edit Drug Information</h1>
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update">
</form>
{% endblock content %}
base.html
{% load static %}
<html>
<head>
<title>Script Tracker Title Base HTML</title>
<link href="https://fonts.googleapis.com/css?family=Tangerine" rel="stylesheet">
<link href="{% static 'css/base.css' %}" rel="stylesheet">
</head>
<body>
<div>
<header>
<div class="nav-left">
<h1>Script Tracker</h1>
</div>
<div class="nav-right">
+ Add New Drug
</div>
</header>
{% block content %}
{% endblock content %}
</div>
</body>
</html>
drug_detail.html
{% extends 'base.html' %}
{% block content %}
<div class="drug-entry">
<h2>{{ post.drug_name }}</h2>
<h2>{{ post.patient_name }}</h2>
</div>
<p>+ Edit Drug Information</p>
<p>+ Delete Drug</p>
{% endblock content %}
Summary
I'm very new to all this and so I apologize if I have not posted all the needed information. I know I'm close and I have something messed up with the pk stuff. http://127.0.0.1:8000/drug/12/edit/ works fine when I type in the browser. Any suggestions would be greatly appreciated.
In your template drug_detail.html you refer to the DrugInfo object as follows: post or drug. While in fact the drugDetailView never passes such variables into the context. A DetailView by default passes the instance as object or <lowercase_model_name> (druginfo here). If you want to set the name it uses you can set context_object_name on the class:
class drugDetailView(DetailView):
model = DrugInfo
template_name = 'drug_detail.html'
context_object_name = 'drug'
Now in the template drug_detail.html:
{% extends 'base.html' %}
{% block content %}
<div class="drug-entry">
<!-- Use `drug` instead of `post` now -->
<h2>{{ drug.drug_name }}</h2>
<h2>{{ drug.patient_name }}</h2>
</div>
<p>+ Edit Drug Information</p>
<p>+ Delete Drug</p>
{% endblock content %}
I am a beginner in Django. Right now, I am building an app, called PhoneReview. It will store reviews related to the latest mobile phone. It will also display phone brands, along with the associated phone models and their reviews.
Right now, I am trying to use slug in URLs. I have successfully used slug in two of my templates, which are index.html and phonemodel.html. However, I am facing issues with the third template, which is details.html.
When I go to http://127.0.0.1:8000/index, I see this page:
When I click on Samsung, I see this page:
Up to this is fine.
But when I click on any phone model, like Galaxy S10, I get FieldError error. It looks like this:
FieldError at /details/galaxy-note-10
Cannot resolve keyword 'slug' into field. Choices are: date_published, id, link, phone_model, review_article
When I click on Samsung, I am supposed to see the details.html page, which has the review of the phone, along with the news link. Instead, I am getting the 404 error.
Here are my codes of models.py located inside PhoneReview folder:
from django.db import models
from django.template.defaultfilters import slugify
# Create your models here.
class Brand(models.Model):
brand_name = models.CharField(max_length=100)
origin = models.CharField(max_length=100)
manufacturing_since = models.CharField(max_length=100, null=True, blank=True)
slug = models.SlugField(max_length=150, null=True, blank=True)
def __str__(self):
return self.brand_name
def save(self, *args, **kwargs):
self.slug = slugify(self.brand_name)
super().save(*args, **kwargs)
class PhoneModel(models.Model):
brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
model_name = models.CharField(max_length=100)
launch_date = models.CharField(max_length=100)
platform = models.CharField(max_length=100)
slug = models.SlugField(max_length=150, null=True, blank=True)
def __str__(self):
return self.model_name
def save(self, *args, **kwargs):
self.slug = slugify(self.model_name)
super().save(*args, **kwargs)
class Review(models.Model):
phone_model = models.ManyToManyField(PhoneModel, related_name='reviews')
review_article = models.TextField()
date_published = models.DateField(auto_now=True)
link = models.TextField(max_length=150, null=True, blank=True)
def __str__(self):
return self.review_article
Here are my codes of urls.py located inside PhoneReview folder:
from . import views
from django.urls import path
app_name = 'PhoneReview'
urlpatterns = [
path('index', views.BrandListView.as_view(), name='brandlist'),
path('phonemodel/<slug:slug>', views.ModelView.as_view(), name='modellist'),
path('details/<slug:slug>', views.ReviewView.as_view(), name='details'),
]
Here are my codes of views.py located inside PhoneReview folder:
from django.shortcuts import render, get_object_or_404
from django.views import generic
from .models import Brand, PhoneModel, Review
class BrandListView(generic.ListView):
template_name = 'PhoneReview/index.html'
context_object_name = 'all_brands'
def get_queryset(self):
return Brand.objects.all()
class ModelView(generic.ListView):
template_name = 'PhoneReview/phonemodel.html'
context_object_name = 'all_model_name'
def get_queryset(self):
self.brand = get_object_or_404(Brand, slug=self.kwargs['slug'])
return PhoneModel.objects.filter(brand=self.brand)
class ReviewView(generic.DetailView):
model = Review
template_name = 'PhoneReview/details.html'
Here are my codes of apps.py located inside PhoneReview folder:
from django.apps import AppConfig
class PhonereviewConfig(AppConfig):
name = 'PhoneReview'
Here are my codes of index.html located inside templates folder:
{% extends 'PhoneReview/base.html' %}
{% load static %}
{% block title%}
Brand List
{% endblock %}
{% block content %}
<!--Page content-->
<h1>This is Brand List Page</h1>
<h2>Here is the list of the brands</h2>
<ul>
{% for brand in all_brands %}
<!-- <li>{{ brand.brand_name }}</li>-->
<li>{{ brand.brand_name }}</li>
{% endfor %}
</ul>
<img src="{% static "images/brandlist.jpg" %}" alt="Super Mario Odyssey" /> <!-- New line -->
{% endblock %}
Here are my codes of phonemodel.html located inside templates folder:
{% extends 'PhoneReview/base.html' %}
{% load static %}
{% block title%}
Phone Model Page
{% endblock %}
{% block content %}
<!--Page content-->
<h1>This is Phone Model Page</h1>
<h2>Here is the phone model</h2>
<ul>
{% for phonemodel in all_model_name %}
<li>{{ phonemodel.model_name }}</li>
{% endfor %}
</ul>
<img src="{% static "images/brandlist.jpg" %}" alt="Super Mario Odyssey" /> <!-- New line -->
{% endblock %}
Here are my codes of details.html located inside templates folder:
{% extends 'PhoneReview/base.html' %}
{% load static %}
<html>
<link rel="stylesheet" type="text/css" href="{% static "css/style.css" %}">
<html lang="en">
{% block title%}Details{% endblock %}
{% block content %}
<h1>This is the Details Page</h1>
<h2>Review:</h2>
<p>{{ review.review_article }}</p>
<h2>News Link:</h2>
<a href={{ review.link }}>{{ review.link }}</a>
{% endblock %}
</html>
Is there anything wrong in models.py or details.html?
am using django 2.0 and here is the problem
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/blog/post-detail/
Raised by: blog.views.post_detail
No Post matches the given query.
here is the blog/urls
from django.urls import path,include
from .import views
urlpatterns = [
path('blog/',views.post_list,name="post_list"),
path('blog/post-detail/',views.post_detail,name="post_detail"),
]
and views.py
from django.shortcuts import render,get_object_or_404
from.models import Post
# Create your views here.
def post_list(request):
object_list=Post.objects.all()
context={
'object_list': object_list,
}
return render(request,"blog.html",context)
def post_detail(request,slug=None):
post=get_object_or_404(Post,slug=slug)
context={
'post':post,
}
return render(request,"post_detail.html",context)
and the post_detail.html
{% extends "base.html" %}
{% load static %}
{% block seo_title %}{% endblock %}
{% block seo_description %}{% endblock %}
{% block Content %}
<article>
<div class="embed-responsive embed-responsive-16by9">
<img src="images/blog1.jpg" alt="" />
</div>
<div class="post-content">
<h2>{{post.title}}</h2>
<div>
{{post.created}} Author {{Post.user}}
<hr/>
<p>{{post.body}}</p>
</article>
{% endblock Content %}
CAN ANYONE HELP ON THIS THE ONLY PROBLEM I SEE THAT SLUG THING I MUST HAVE CONFUSED SOMEWHERE
blog.html
<!-- Blog -->
<div class="blog">
<div class="row">
<div class="col-sm-8">
<!-- Blog Post-->
{% for obj in object_list %}
{% if obj.status == 'Published' %}
<article>
<div class="embed-responsive embed-responsive-16by9">
<img src="images/blog1.jpg" alt="" />
</div>
<div class="post-content">
<h2>{{obj.title}}</h2>
<div>
{{obj.created}} Author {{obj.user}}
<hr/>
<p>{{obj.body}}</p>
<a class="mtr-btn button-navy ripple" href= "{% url 'post_detail' slug= post.slug %}">Continue reading →</a><br>
</div>
</article>
{% endif %}
{% endfor %}
The view post_detail(request,slug=None) is to view details about a post. So your URL pattern is incorrect:
path('blog/post-detail/<slug:slug>',views.post_detail,name="post_detail"),
To call it in templates, the simpler and correct way to do is:
<a class="mtr-btn button-navy ripple" href= "{% url 'post_detail' obj.slug %}">Continue reading →</a><br>
</div>
#FOLLOW THIS PROCEDURE.I HOPE IT HELPS YOU OR ANY ONE ELSE IN THE FUTURE
# At blog/urls.py
from django.urls import path
from .views import (post_list, post_detail)
urlspatterns = [
path('blog/', post_list, name='post-list'),
path('<str:slug>/blog/post-detail/', post_detail, name='post-detail'),
]
#At blog/views.py
from django.shortcuts import render, get_object_or_404
from .models import Post
def post_list(request):
posts = Post.objects.all()
template_name = blog/post_list.html
context = {'posts':posts}
return render(request, template_name, context)
def post_detail(request, slug):
posts = get_object_or_404(Post, slug=slug)
template_name = blog/post_detail.html
context = {'posts':posts}
return render(request, template_name, context)
# At the template/blog/post_list.html
{% block content %}
{% for post in posts %}
<article>
<div>
<small>{{ post.created_on|date:"F d, Y" }}</small>
<h2>{{ post.title }}</h2>
<p >{{ post.body }}</p>
</div>
</article>
{% endfor %}
{% endblock content %}
# At template/blog/post_detail.html
<article>
<div>
<small>{{ posts.created_on|date:"F d, Y" }}</small>
<h2>{{ posts.title }}</h2>
<p>{{ posts.body }}</p>
</div>
</article>
#The above code should fix the the issue properly.
If your a following Django 3 By Example Book can check this soltuion because I had the same problem.
Book teach how to create Post with a slug attribute with
models.SlugField and unique_for_date='pusblish' condition.
Then you add some posts from admin site.
Then you add some posts from admin site but then in admin.py book teach how to edit the register with
prepopulated_fields = {'sulg':('title',)}.
Finally, book teach you how to edit posts.
This is a problem because never
going to find a post created. So, the solution for me was delete
posts and the create new ones.
here my code:
models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
class PublishedManager(models.Manager):
def get_queryset(self):
return super(PublishedManager, self).get_queryset().filter(status='published')
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique_for_date='publish')
author = models.ForeignKey(User,
on_delete=models.CASCADE,
related_name='blog_posts')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
objects = models.Manager() # The default manager.
published = PublishedManager() # Our custom manager.
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:post_detail',
args=[self.publish.year,
self.publish.month,
self.publish.day, self.slug])
admin.py
from django.contrib import admin
from .models import Post
#admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'slug', 'author', 'publish', 'status')
list_filter = ('status', 'created', 'publish', 'author')
search_fields = ('title', 'body')
prepopulated_fields = {'slug': ('title',)}
raw_id_fields = ('author',)
date_hierarchy = 'publish'
ordering = ('status', 'publish')
views.py
from django.shortcuts import render, get_object_or_404
# another views...
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug=post,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
return render(request,
'blog/post/detail.html',
{'post': post})
blog/urls.py
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
# post views
path('', views.PostListView.as_view(), name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/',
views.post_detail,
name='post_detail'),
]
my_site/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls', namespace='blog')),
]
I am very new to Django and have been following through the blog tutorial from the book: "Django by Example."
The code below should return all blog posts with a status of "published" but it refuses to work. What appears to be the correct page loads up using the list.html code but there are no posts showing. I have double checked and I am creating posts with the admin site with the status set to "published." I am not sure if the problem is with the template, model, views or URLs so I am including it all here:
Models.py:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify
from django.core.urlresolvers import reverse
#Data model class for creating posts table enrties in database
#Custom manager. Limits returned querysets to ones with published status
class PublishedManager(models.Manager):
def get_queryset(self):
return super(PublishedManager, self).get_queryset()\
.filter(status='published') #super used to call get_queryset method from parent class as it is currently being
#overridden here.
class Post(models.Model):
STATUS_CHOICES = (('draft', 'Draft'), ('published', 'Published'),)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique_for_date='publish') #A field for use in building urls
#slug = models.SlugField(slugify(title))
author = models.ForeignKey(User, related_name='blog_posts') #Each post written by a user and a user can write many posts
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
objects = models.Manager() #The default manager
published = PublishedManager() #Custom manager
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:post_detail', args=[self.publish.year, self.publish.strftime('%m'),self.publish.strftime('%d'), self.slug])
views.py:
from django.shortcuts import render, get_object_or_404
from .models import Post
def post_list(request):
posts = Post.objects.all()
return render(request, 'blog/post/list.html', {'Posts': 'posts'})
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug=post, status='published', publish__year=year, publish__month=month, publish__day=day)
return render(request, 'blog/post/detail.html', {'post':post})
URLs.py
from django.conf.urls import url
from . import views
urlpatterns = [
# post views
url(r'^$', views.post_list, name='post_list'),
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/'\
r'(?P<post>[-\w]+)/$', views.post_detail, name='post_detail'),
Templates base.html:
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
<link href="{% static "css/blog.css" %}" rel="stylesheet">
</head>
<body>
<div id="content">
{% block content %}
{% endblock %}
</div>
<div id="sidebar">
<h2>My Site site</h2>
<p>This site is all about me</p>
</div>
</body>
</html>
detail.html:
{% extends "blog/base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>Blog</h1>
{% for post in posts %}
<h2>
<a href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
<p class="date">Published{{ post.publish }} by {{ post.author }}</p>
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% endblock %}
You need to use Post.published.all() to use your custom manager then in your dict you are doing 'Posts' with a capital P you might want to try lowercase as your view is using posts. Then also the value for it was a string instead of the variable posts. So something like this should work
def post_list(request):
posts = Post.published.all()
return render(request, 'blog/post/list.html', {'posts': posts})
You are passing in a string as your context object data:
return render(request, 'blog/post/list.html', {'Posts': 'posts'})
Needs to be
return render(request, 'blog/post/list.html', {'Posts': posts})
It also still isn't going to work since you have in your view and aren't calling the manager method. You have:
posts = Post.objects.all()
You need to use your customer manager.
I found the answer - all posts in the database are in the "draft" status, and status='published' is registered in the PublishedManager. Change status=''draft" and everything will be OK!