Post detail is missing
I am new to Django, when I follow the instructions on Django 2 by example by Antonio Mele. I met a problem that I can not look up the detail of every post.
Click on the title of the post to take a look at the detail of a post, I see
I hang around to find inspirations on StackOverflow, seems everyone's problems are so different. Relevant files listed as below
blog/views.py
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})
class PostListView(ListView):
queryset = Post.published.all()
context_object_name = 'posts'
paginate_by = 3
template_name = 'blog/post/list.html'
blog/urls.py
urlpatterns = [
path('', views.PostListView.as_view(), name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/',
views.post_detail,
name='post_detail'),
]
blog/templates/blog/post/detail.html
{% extends "blog/base.html" %}
{% block title %}{{ post.title }}{% endblock %}
{% block content %}
<h1>{{ post.title }}</h1>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|linebreaks }}
{% endblock %}
The developing environment is:
Python 3.6.5
Django 2.1.2
Thank Daniel Roseman
blog/templates/blog/post/list.html
{% extends "blog/base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>My 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 %}
{% include "pagination.html" with page=page_obj %}
{% endblock %}
The models.py
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])
If possible, say as much as possible how it works in plain words, Since I am still confused about the mechanism behind Django.
It's confusing, I change the database server back to sqlite3, and the problem disappeared.
django
|
show 2 more comments
I am new to Django, when I follow the instructions on Django 2 by example by Antonio Mele. I met a problem that I can not look up the detail of every post.
Click on the title of the post to take a look at the detail of a post, I see
I hang around to find inspirations on StackOverflow, seems everyone's problems are so different. Relevant files listed as below
blog/views.py
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})
class PostListView(ListView):
queryset = Post.published.all()
context_object_name = 'posts'
paginate_by = 3
template_name = 'blog/post/list.html'
blog/urls.py
urlpatterns = [
path('', views.PostListView.as_view(), name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/',
views.post_detail,
name='post_detail'),
]
blog/templates/blog/post/detail.html
{% extends "blog/base.html" %}
{% block title %}{{ post.title }}{% endblock %}
{% block content %}
<h1>{{ post.title }}</h1>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|linebreaks }}
{% endblock %}
The developing environment is:
Python 3.6.5
Django 2.1.2
Thank Daniel Roseman
blog/templates/blog/post/list.html
{% extends "blog/base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>My 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 %}
{% include "pagination.html" with page=page_obj %}
{% endblock %}
The models.py
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])
If possible, say as much as possible how it works in plain words, Since I am still confused about the mechanism behind Django.
It's confusing, I change the database server back to sqlite3, and the problem disappeared.
django
Show list.html, especially the part where you are creating the links to those detail pages.
– Daniel Roseman
Nov 2 '18 at 9:00
OK, the link is generated by get_absolute_url, so you need to show the model.
– Daniel Roseman
Nov 2 '18 at 9:06
Does an object fitting the given parameters exist in the database? What you are seeing isget_object_or_404
returning aHttp404
exception instead of aPost.DoesNotExist
exception.
– CoffeeBasedLifeform
Nov 2 '18 at 9:23
@CoffeeBasedLifeform but the point is OP is following the link from the index page, which is generated from an existing model instance, so it's not clear what is going on.
– Daniel Roseman
Nov 2 '18 at 9:33
Does it work if you removestatus='published'
from the query inpost_detail
?
– Daniel Roseman
Nov 2 '18 at 9:33
|
show 2 more comments
I am new to Django, when I follow the instructions on Django 2 by example by Antonio Mele. I met a problem that I can not look up the detail of every post.
Click on the title of the post to take a look at the detail of a post, I see
I hang around to find inspirations on StackOverflow, seems everyone's problems are so different. Relevant files listed as below
blog/views.py
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})
class PostListView(ListView):
queryset = Post.published.all()
context_object_name = 'posts'
paginate_by = 3
template_name = 'blog/post/list.html'
blog/urls.py
urlpatterns = [
path('', views.PostListView.as_view(), name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/',
views.post_detail,
name='post_detail'),
]
blog/templates/blog/post/detail.html
{% extends "blog/base.html" %}
{% block title %}{{ post.title }}{% endblock %}
{% block content %}
<h1>{{ post.title }}</h1>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|linebreaks }}
{% endblock %}
The developing environment is:
Python 3.6.5
Django 2.1.2
Thank Daniel Roseman
blog/templates/blog/post/list.html
{% extends "blog/base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>My 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 %}
{% include "pagination.html" with page=page_obj %}
{% endblock %}
The models.py
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])
If possible, say as much as possible how it works in plain words, Since I am still confused about the mechanism behind Django.
It's confusing, I change the database server back to sqlite3, and the problem disappeared.
django
I am new to Django, when I follow the instructions on Django 2 by example by Antonio Mele. I met a problem that I can not look up the detail of every post.
Click on the title of the post to take a look at the detail of a post, I see
I hang around to find inspirations on StackOverflow, seems everyone's problems are so different. Relevant files listed as below
blog/views.py
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})
class PostListView(ListView):
queryset = Post.published.all()
context_object_name = 'posts'
paginate_by = 3
template_name = 'blog/post/list.html'
blog/urls.py
urlpatterns = [
path('', views.PostListView.as_view(), name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/',
views.post_detail,
name='post_detail'),
]
blog/templates/blog/post/detail.html
{% extends "blog/base.html" %}
{% block title %}{{ post.title }}{% endblock %}
{% block content %}
<h1>{{ post.title }}</h1>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|linebreaks }}
{% endblock %}
The developing environment is:
Python 3.6.5
Django 2.1.2
Thank Daniel Roseman
blog/templates/blog/post/list.html
{% extends "blog/base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>My 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 %}
{% include "pagination.html" with page=page_obj %}
{% endblock %}
The models.py
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])
If possible, say as much as possible how it works in plain words, Since I am still confused about the mechanism behind Django.
It's confusing, I change the database server back to sqlite3, and the problem disappeared.
django
django
edited Nov 16 '18 at 11:47
ComplicatedPhenomenon
asked Nov 2 '18 at 8:57
ComplicatedPhenomenonComplicatedPhenomenon
73210
73210
Show list.html, especially the part where you are creating the links to those detail pages.
– Daniel Roseman
Nov 2 '18 at 9:00
OK, the link is generated by get_absolute_url, so you need to show the model.
– Daniel Roseman
Nov 2 '18 at 9:06
Does an object fitting the given parameters exist in the database? What you are seeing isget_object_or_404
returning aHttp404
exception instead of aPost.DoesNotExist
exception.
– CoffeeBasedLifeform
Nov 2 '18 at 9:23
@CoffeeBasedLifeform but the point is OP is following the link from the index page, which is generated from an existing model instance, so it's not clear what is going on.
– Daniel Roseman
Nov 2 '18 at 9:33
Does it work if you removestatus='published'
from the query inpost_detail
?
– Daniel Roseman
Nov 2 '18 at 9:33
|
show 2 more comments
Show list.html, especially the part where you are creating the links to those detail pages.
– Daniel Roseman
Nov 2 '18 at 9:00
OK, the link is generated by get_absolute_url, so you need to show the model.
– Daniel Roseman
Nov 2 '18 at 9:06
Does an object fitting the given parameters exist in the database? What you are seeing isget_object_or_404
returning aHttp404
exception instead of aPost.DoesNotExist
exception.
– CoffeeBasedLifeform
Nov 2 '18 at 9:23
@CoffeeBasedLifeform but the point is OP is following the link from the index page, which is generated from an existing model instance, so it's not clear what is going on.
– Daniel Roseman
Nov 2 '18 at 9:33
Does it work if you removestatus='published'
from the query inpost_detail
?
– Daniel Roseman
Nov 2 '18 at 9:33
Show list.html, especially the part where you are creating the links to those detail pages.
– Daniel Roseman
Nov 2 '18 at 9:00
Show list.html, especially the part where you are creating the links to those detail pages.
– Daniel Roseman
Nov 2 '18 at 9:00
OK, the link is generated by get_absolute_url, so you need to show the model.
– Daniel Roseman
Nov 2 '18 at 9:06
OK, the link is generated by get_absolute_url, so you need to show the model.
– Daniel Roseman
Nov 2 '18 at 9:06
Does an object fitting the given parameters exist in the database? What you are seeing is
get_object_or_404
returning a Http404
exception instead of a Post.DoesNotExist
exception.– CoffeeBasedLifeform
Nov 2 '18 at 9:23
Does an object fitting the given parameters exist in the database? What you are seeing is
get_object_or_404
returning a Http404
exception instead of a Post.DoesNotExist
exception.– CoffeeBasedLifeform
Nov 2 '18 at 9:23
@CoffeeBasedLifeform but the point is OP is following the link from the index page, which is generated from an existing model instance, so it's not clear what is going on.
– Daniel Roseman
Nov 2 '18 at 9:33
@CoffeeBasedLifeform but the point is OP is following the link from the index page, which is generated from an existing model instance, so it's not clear what is going on.
– Daniel Roseman
Nov 2 '18 at 9:33
Does it work if you remove
status='published'
from the query in post_detail
?– Daniel Roseman
Nov 2 '18 at 9:33
Does it work if you remove
status='published'
from the query in post_detail
?– Daniel Roseman
Nov 2 '18 at 9:33
|
show 2 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53115417%2fpost-detail-is-missing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53115417%2fpost-detail-is-missing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Show list.html, especially the part where you are creating the links to those detail pages.
– Daniel Roseman
Nov 2 '18 at 9:00
OK, the link is generated by get_absolute_url, so you need to show the model.
– Daniel Roseman
Nov 2 '18 at 9:06
Does an object fitting the given parameters exist in the database? What you are seeing is
get_object_or_404
returning aHttp404
exception instead of aPost.DoesNotExist
exception.– CoffeeBasedLifeform
Nov 2 '18 at 9:23
@CoffeeBasedLifeform but the point is OP is following the link from the index page, which is generated from an existing model instance, so it's not clear what is going on.
– Daniel Roseman
Nov 2 '18 at 9:33
Does it work if you remove
status='published'
from the query inpost_detail
?– Daniel Roseman
Nov 2 '18 at 9:33