博客详细信息模板不存在错误

时间:2021-07-07 11:59:23

标签: django django-views

我有一个简单的博客应用,它有一个博客列表视图和自动生成的详细信息页面。在本地,一切似乎都很好。但是,当我在服务器上部署应用程序后,当我单击查看详细信息页面时,我收到一个模板不存在错误。 这是我的观点:

class BlogMain(ListView):
    model = BlogPost
    template_name = 'Blog/Blog-news.html'
    queryset = BlogPost.objects.all()
    context_object_name = 'posts'
    ordering = ['-published']
    paginate_by = 3

    def get_context_data(self, **kwargs):
        context = super(BlogMain, self).get_context_data(**kwargs)
        context['tags'] = BlogPost.tags.all()
        return context


class BlogTags(ListView):
    model = BlogPost
    template_name = 'Blog/Blog-news.html'
    context_object_name = 'posts'

    def get_queryset(self):
        return BlogPost.objects.filter(tags__slug=self.kwargs.get('tag_slug'))

    def get_context_data(self, **kwargs):
        context = super(BlogTags, self).get_context_data(**kwargs)
        context['tags'] = BlogPost.tags.all()
        return context


class BlogDetail(DetailView):
    model = BlogPost
    template_name = 'Blog/Blog-news-detail.html'
    context_object_name = 'blog'
    slug_url_kwarg = 'the_slug'
    slug_field = 'slug'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["related_items"] = self.object.tags.similar_objects()[:3]
        return context

这是我的网址:

path('blog/', views.BlogMain.as_view(), name="blog-page"),
path('log/tags/<slug:tag_slug>/', views.MaghaleTags.as_view(), name="tagged"),
re_path(r'blog/(?P<the_slug>[-\w]+)/', views.BlogDetail.as_view(), name='post-detail'),

最后是我的模板:

{% for post in posts %}
<div class="container-fluid blog-main mb-5">
    <a href="{% url 'post-detail' post.slug %}">
        <div class="row">
            <div class="col-lg-5 pt-5 pb-3 my-5">
                <h5>{{ post.title | persianize_digits }}</h5>
                <small>
                    {% for tag in post.tags.all %}
                    {{ tag.name }}
                    {% endfor %}
                </small>
                <br>
            </div>
            
        </div>
    </a>
</div>
{% endfor %}

1 个答案:

答案 0 :(得分:0)

我确定这是您的问题,我将分享我的经验。我将我的模板命名为 blog.html,在我编写的代码中,我编写了 Blog.html 刚开始字符大写。它在我的本地服务器上运行良好,但是当我在 pythonanywhere.com 上部署网站时,它给了我同样的错误模板不存在<​​/strong>,所以我将模板名称更改为 blog.html 在代码中并且它起作用了。看看您是否遇到这种问题。