Я делаю учебник "Блог" из "Django 3 на примере", и я получил ошибку. Что я делаю не так?
Ошибка: страница не найдена (404) Метод запроса: GET URL запроса: http://127.0.0.1: 8000 / blog / amd alsp TemplateDoesNotExist в / blog /. Используя URLconf, определенный в mysite.urls, Django попробовал эти шаблоны URL в следующем порядке: admin / текущий URL, blog /, не соответствует ни одному из них.
BLOG
ADMIN
mysite / urls.py
from django.contrib import admin
from django.urls import path, include, re_path
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls', namespace='blog')),
]
mysite / blog / views.py
from django.shortcuts import render, get_list_or_404
from .models import Post
# Create your views here.
def post_list(request):
posts = Post.published.all()
return render(request, 'blog/post/list.html', {'post': posts})
def post_detail(request, year, month, day, post):
post = get_list_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})
mysite / blog / admin.py
from django.contrib import admin
from .models import Post
# Register your models here.
@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')
mysite / mysite / urls.py
from django.urls import path, include
from mysite.blog import admin
from . import views
app_name = 'blog'
urlpatterns = [
# post views
path('', views.post_list, name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/',
views.post_detail, name='post_detail'),
]
mysite / blog / models
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
# Create your models here.
class PublishedManager(models.Manager):
def get_queryset(self):
return super().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')
object = models.Manager()
published = PublishedManager()
def get_absolute_url(self):
return reverse('blog:post_detail', args=[self.publish.year, self.publish.month, self.publish.day, self.slug])
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title