【发布时间】:2020-06-06 23:31:00
【问题描述】:
this is the error messag am getting in a picture 有人应该帮我解决这个问题,我在过去 3 天里一直在解决这个问题, 我有一个由多个作者或管理员处理的博客,所以我决定使用基于类的视图,当我创建新帖子时出现错误说我需要 blog_detail.html,因为我没有详细的博客,所以我创建了一个 Detailview然后添加我的 template_name = 'mymainblogpage.html' 所以一旦我创建新帖子,它将被发布但不会显示帖子但只会显示分页,除了我无法按帖子 ID 搜索例如 http://127.0.0.1:8000/pages/blog/49/ 我什么都没有得到我的代码。
views.py
def blog(request):
blog_post = Blog.objects.all()
ordering = ['-timestamp']
paginator = Paginator(blog_post, 2)
page_request_var = 'page'
page = request.GET.get(page_request_var)
try:
paginated_queryset = paginator.page(page)
except PageNotAnInteger:
paginated_queryset = paginator.page(2)
except EmptyPage:
paginated_queryset = paginator.page(paginator.num_pages)
context = {
'queryset': paginated_queryset,
'page_request_var': page_request_var
}
return render(request, 'pages/blog.html', context)
class BlogDetailView(DetailView):
model = Blog
template_name = 'pages/blog.html'
context_object_name = 'blog'
urls.py
> path('blog/', BlogDetailView.as_view(), name='blog'),
path('blog/<int:pk>/', BlogDetailView.as_view(), name='blog-detail'),
models.py
> def get_absolute_url(self):
return reverse('blog-detail', kwargs={'pk': self.pk})
blog.html
<br><p></p>
<section class="ftco-section bg-light" id="blog-section">
<div class="container">
<div class="row justify-content-center mb-5 pb-5">
<div class="col-md-10 heading-section text-center ftco-animate">
<h2 class="mb-4">Gets Every Single Updates Here</h2>
<p>Far far away, behind the word mountains, far from the countries Vokalia and Consonantia</p>
</div>
</div>
<div class="container">
<div class="row justify-content-center mb-5 pb-5">
<div class="col-lg-8 ftco-animate">
{% for blog in queryset %}
<h2 class="text-center">{{ blog.title }}</h2>
<div class="meta mb-3 text-center">
<div><h6><span><a href = "">written By {{ blog.user }}</span><small class="date"><i class="icon-clock"></i> {{ blog.timestamp|timesince }} ago</small><a/></h6>
</div>
</div>
<div><small class="icon-eye text-danger">{{ blog.view_count }}</small></div>
<div class="meta mb-3 text-center">
<h5>{% for cat in blog.categories.all %}<span class="btn btn-dark">{{ cat }}</span> {% endfor %}</h5>
</div>
<p class="text-center">{{ blog.overview }}</p>
{% endfor %}
<nav aria-label="pagination">
<ul class="pagination pagination-circle pg-blue">
{% if queryset.has_previous %}
<li class="page-item"><a href= "?{{ page_request_var }}={{ queryset.previous_page_number }}" class="page-link">Previous</a></li>
{% endif %}
<li class="page-item active"><a href= "?{{ page_request_var }}={{ queryset.number }}" class="page-link">{{ queryset.number }}</a></li>
</a>
</li>
{% if queryset.has_next %}
<li class="page-item"><a href= "?{{ page_request_var }}={{ queryset.next_page_number }}" class="page-link">Next</a></li>
{% endif %}
</ul>
</nav>
{% if is_paginated %}
<nav aria-label="pagination">
<ul class="pagination pagination-circle pg-blue">
{% if queryset.has_previous %}
<li class="page-item"><a href= "?{{ page_request_var }}={{ queryset.previous_page_number }}" class="page-link">Previous</a></li>
{% endif %}
<li class="page-item active"><a href= "?{{ page_request_var }}={{ queryset.number }}" class="page-link">{{ queryset.number }}</a></li>
</a>
</li>
{% if queryset.has_next %}
<li class="page-item"><a href= "?{{ page_request_var }}={{ queryset.next_page_number }}" class="page-link">Next</a></li>
{% endif %}
</ul>
</nav>
{% endif %}
</div>
</div>
</div>
</div>
</section>
【问题讨论】:
-
您的
pages/blog.html文件是什么样的? -
它是我的博客页面,我已经建立了它,但只显示标题、作者、类别和帖子的概述,然后在下分页