Fred69-coder
Fred69-coder

Reputation: 11

Django 404 Error Raised by: blog.views.post_detail

Hello, Everyone

I'm a novice programmer trying to follow Antonio Mele tutorial on blog creation using django.

Am stuck with the post/detail.html which is not responding below are the codes.Kindly help.

Problem: Am unable to view details of the post

404 Error: Page not found

Page not found (404)

Request Method: GET

Request URL: http://127.0.0.1:8000/blog/2020/3/13/more-post/

Raised by: blog.views.post_detail

No Post matches the given query.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

Error:Page not found

Page not found (404)

Request Method: GET

Request URL: http://127.0.0.1:8000/

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:

    1.admin/

    2.blog/

The empty path didn't match any of these. You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.*

urls.py/blog

urls.py/mysite

views.py

urls.py/blog

from django.urls import path
from . import views

app_name = 'blog'

urlspatterns = [
    path('', views.post_list, name='post_list'),
    path('<int:year>/<int:month>/<int:day>/<slug:post>/', 
        views.post_detail, 
        name='post_detail'),

]

urls.py/mysite

from django.contrib import admin
from django.urls import path, include



urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls', namespace='blog')),

]

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})

Upvotes: 1

Views: 1194

Answers (3)

Abayomi Olowu
Abayomi Olowu

Reputation: 349

#FOLLOW THIS PROCEDURE.I HOPE IT HELPS YOU OR ANY ONE ELSE IN THE FUTURE

#blog/models.py
from django.db import models
from django.utils import timezone


 class Post(models.Model):
    published = models.BooleanField(True)
    created_on = models.DateTimeField(auto_now=timezone.now)

# At blog/urls.py
from django.urls import path
from .views import (post_list, post_detail)


urlspatterns = [
    path('', post_list, name='post_list'),
    path('<str:slug>/', post_detail, name='post_detail'),


#At mysite/urls.py
from django.contrib import admin
from django.urls import path, include



urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls')),

#At blog/views.py
from django.shortcuts import render, get_object_or_404
from .models import Post


def post_list(request):
    posts = Post.objects.filter(published) 
    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><a href="{{ post.slug }}">{{ post.title }}</a></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.

Upvotes: 0

Letouane
Letouane

Reputation: 63

Someone had the same problem with the previous version of the book, asked here and published his solution (there must be a much more elegant way), have a look here: Django get_object_or_404() with DateTimeField

In addition, use the shell to debug items, you should have seen that the date in DB is recorded in UTC:

>>> post = Post.objects.get(title__startswith='who')
>>> post.publish
datetime.datetime(2020, 8, 18, 1, 18, 56, tzinfo=<UTC>)

Upvotes: 1

Barsukoff
Barsukoff

Reputation: 1

Take a look at the admin area, where the articles have a 'draft' status. Change them to 'published' status. In the views.py in the function post_list(), change the output of posts on a page with only the status published:

def post_list(request):    
    posts = Post.objects.filter(status='published')
    return render(request, 'blog/post/list.html', {'posts': posts})

In this case, articles with only the status published are displayed on the posts page. By clicking on the article link, you should be transferred to a full post without error.

Upvotes: 0

Related Questions