0

I have, Page not found (404) when accessing /category/Django

urls.py

from .views import (
    PostListViewHome,
    CategoryView,

)

from . import views

urlpatterns = [
    path("", PostListViewHome.as_view(), name="Blog-home"),  
    path("category/<str:cats>/", views.CategoryView, name="category"),
]

views.py

def CategoryView(request, cats):
    category_posts = Post.objects.filter(category=cats)
    return render(request, 'Blog/category.html', {'cats':cats, 'category_posts':category_posts})

urls.py in the MPIE02

from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import path, include
from django.views.generic.base import TemplateView
from users import views as user_views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
          path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='Logout'),
    path('', include ('Blog.urls')),
]


if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

if i try to accessing /category/ it showin me just 404

Zak Meshou
  • 25
  • 7

1 Answers1

0

You are getting this error because in your settings file, there is no any path added of templates.

Add path in settings file:

 import os
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR,'appname','templates',)],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]
Manoj Tolagekar
  • 1,816
  • 2
  • 5
  • 22