Traceback (most recent call last):
....
raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: None
Django项目。
settings.py
TEMPLATES = [
{ ...
'DIRS': [ os.path.join(BASE_DIR, 'templates')],},
...]
项目树中的路径:blog / template / blog(此处为所有.html文件)
blog / views.py
from django.views.generic import View
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from .models import Post, Tag
from .utils import ObjectDetailMixin
def posts_list(request):
posts = Post.objects.all()
return render(request, "blog/index.html", context={'posts':posts})
class PostDetail(ObjectDetailMixin, View):
model = Post
tamplate = 'blog/post_detail.html'
class TagDetail(ObjectDetailMixin, View):
model = Tag
template = 'blog/tag_detail.html'
def tags_list(request):
tags = Tag.objects.all()
return render(request,'blog/tags_list.html',
context={'tags': tags})
blog / utils.py
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from .models import *
class ObjectDetailMixin:
model = None
template = None
def get(self, request, slug):
obj = get_object_or_404(self.model,
slug__iexact=slug)
return render(request, self.template,
context={self.model.__name__.lower():obj})
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
]
blog / urls.py
from django.urls import path
from .views import *
urlpatterns = [
path('', posts_list, name='posts_list_url'),
path('post/<str:slug>/', PostDetail.as_view(),
name='post_detail_url'),
path('tags/', tags_list, name='tags_list_url'),
path('tag/<str:slug>', TagDetail.as_view(), name='tag_detail_url')
]
blog / models.py
from django.db import models
from django.shortcuts import reverse
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=150, db_index=True)
slug = models.SlugField(max_length=150, unique=True)
body = models.TextField(blank=True)
tags =
models.ManyToManyField('Tag',blank=True,related_name='posts')
date_pub = models.DateTimeField(auto_now_add=True)
def get_absolute_url(self):
return reverse('post_detail_url', kwargs={'slug':self.slug})
def __str__(self):
return self.title
class Tag(models.Model):
title = models.CharField(max_length=50)
slug = models.SlugField(max_length=50,unique=True)
def get_absolute_url(self):
return reverse('tag_detail_url', kwargs={'slug': self.slug})
def __str__(self):
return self.title
post_card_template.html
<div class="card mb-4">
<div class="card-header">
{{ post.date_pub}}
</div>
<div class="card-body">
<h5 class="card-title">{{post.title}}</h5>
<p class="card-text">{{post.body|truncatewords:10}}</p>
<a href="{{ post.get_absolute_url }}" class="btn btn-light">Read</a>
</div>
<div class="card-footer text-muted">
Tags:
{% for tag in post.tags.all %}
<a href="{{ tag.get_absolute_url }}"> {{ tag.title }}</a>
{% endfor %}
</div>
您能帮我解决问题吗?请
答案 0 :(得分:0)
您在PostDetail
中有一个错字-您设置了tamplate
而不是template
。
class PostDetail(ObjectDetailMixin, View):
model = Post
tamplate = 'blog/post_detail.html'