I'm using Django 2.1 and testing views.py and urls.py What I don't understand is why whenever I enter the URL http://127.0.0.1:8000/blog/post_list I get a 404 error message
My top urls.py:
from django.urls import path
from django.contrib import admin
from django.conf.urls import include, url
from organizer import urls as organizer_urls
from blog import urls as blog_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(organizer_urls)),
path('tag/', include(organizer_urls)),
path('startup/', include(organizer_urls)),
path('blog/', include(blog_urls))
]
my application's urls.py
from django.urls import path
from blog.views import post_list, post_detail
urlpatterns = [
path('',
post_list,
name='blog_post_list'),
path(
'<int:year>/<int:month>/<slug:slug>',
post_detail,
name='blog_post_detail'),
]
my application's views.py:
from django.shortcuts import render, get_object_or_404
from .models import Post
# Create your views here.
def post_list(request):
return render(
request,
'blog/post_list.html',
{'post_list':Post.object.all()}
)
def post_detail(request, year, month, slug):
post = get_object_or_404(
Post,
pub_date__year=year,
pub_date__month=month,
slug=slug)
return render(
request,
'blog/post_detail.html',
{'post': post})
the error message is:
Using the URLconf defined in suorganizer_project.urls, Django tried these URL patterns, in this order:
admin/ tag/tag_list [name='organizer_tag_list'] tag// startup/startup_list [name='organizer_startup_list'] startup// [name='organizer_startup_detail'] tag/ startup/ blog/ The empty path didn't match any of these.
Why ? The url is there:
path('',
post_list,
name='blog_post_list'),
which would take me to post_list views:
def post_list(request):
return render(
request,
'blog/post_list.html',
{'post_list':Post.object.all()}
)
and returns a query for all objects in post --Post.object.all()-- ?
I don't understand what i'm missing, would appreciate your help ! :)
post_list
is not in your URL pattern, but it is your function name. If you access the URL that way: 127.0.0.1:8000/blog/
it will work and call post_list()
view based on this pattern
path('',
post_list,
name='blog_post_list'),
If you want to have your URL works, just edit your pattern the following way by adding the post_list
string.
path('post_list',
post_list,
name='blog_post_list'),