Reputation: 3
I'm doing tutorial "Blog" from "Django 3 by example" and I got error. What i'm doing wrong?
Error: Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/blog/ amd alsp TemplateDoesNotExist at /blog/. Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: admin/ The current URL, blog/, didn't match any of these.
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
Upvotes: 0
Views: 3346
Reputation: 677
The first error of the template not found is because of its path is not specified in the settings.py
file.
And second one is because of you have not specified /
path in your mysite/urls.py
If you want more explanation, feel free to ask.
Upvotes: 0