Py学习  »  Git

如何迭代我的postlist来显示taggit中的标签?

Uros Dobricic • 4 年前 • 760 次点击  

我想迭代我的博文列表,在单击所有具有相同标签的博文时显示。 我希望它们显示在index.html上。 我理解这个函数,但我不知道如何将它实现到我的模板中。

我的观点.py

    from django.views import generic
from .models import Post
from django.shortcuts import render

class PostList(generic.ListView):
    model = Post
    template_name = 'index.html'

class PostDetail(generic.DetailView):
    model = Post
    template_name = 'post_detail.html'

def tag(request, slug):
    posts = Post.objects.filter(tags__slug=tag)
    return render(request, 'index.html', {"post_list": posts, "tag": tag})

def about(request):
    return render(request, 'about.html', {})

我的模板

 <header class="masthead" >
            <div class="overlay"></div>
            <div class="container">
              <div class="row">
                  <div class="site-heading">
                    <h5 class=" site-heading mt-3"> Erfolgreiche Problemlösungen in der Programmierung, Linux und Windows</h5>
                    <p class="texthead">
                    </p>
                </div>
                    </div>
                  </div>
                </div>
            </div>
              </header>

            <div class="container">
                <div class="row">

              <!-- Blog Entries Column -->
              <div class="col-md-8 mt-3 left">
              {% for post in post_list %}
                <div class="card mb-4" >
                  <div class="card-body">
                    <h2 class="card-title">{{ post.title }}</h2>
                    <p class="card-text text-muted h6">{{ post.author }} | {{ post.created_on | date:"d M Y"}} | Tag:
                        {% for tag in post.tags.all %}
                            <a class="mycardtext" href="{% url 'tag' tag.slug %}">{{ tag.name }}</a>
                        {% empty %}
                          None
                        {% endfor %}
                        </p>

                        <p class="card-text">{{post.content|truncatewords:25|safe}}</p>
                        <div><a href="{% url 'post_detail' post.slug  %}"
                        class="btn btn-danger">Weiterlesen</a></h3>
                    </div>
                  </div>

                </div>
                {% endfor %}
            </div>
                {% block sidebar %}
                {% include 'sidebar.html' %}
                {% endblock sidebar %}
            </div>
        </div>
{%endblock%}

我的网址.py

from . import views
from django.urls import path
from django.urls import include
from django.views.generic.base import RedirectView



favicon_view = RedirectView.as_view(url='/home/UrosDobricic/mysite/static/favicon.png', permanent=True)

urlpatterns = [
    path('', views.PostList.as_view(), name='home'),
    path('about', views.about, name='about'),
    path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
    path("tag/<slug:slug>/", views.tag, name='tag'),

]

标签显示在博客上。我的问题是,我在views.py中创建了这个post-list=post.objects.filter(tags-slug=tag),但是在我的模板中没有定义post。 所以当我在post.tags.all%to%中为post list.tags.all%中的标记定义部分时,它会在我的博客中显示“无”。 我找不到合适的解决方案,所以我想问一下有没有人给我小费。 一般来说,这意味着我必须展示

%用于后_list%中的标签

标签

%endfor%

还是我错了? 如果有人能根据我的文件用一个例子来回答,那就太好了。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38216
 
760 次点击  
文章 [ 1 ]  |  最新文章 4 年前
user11330156
Reply   •   1 楼
user11330156    4 年前

多亏了德克格罗坦,我在他的帮助下解决了我的问题!