M
M
Maxemp2017-09-15 16:10:03
Django
Maxemp, 2017-09-15 16:10:03

Why is the tag not showing?

class Post(models.Model):
    title = models.CharField(max_length=200)
    text = models.TextField()
    date_pub = models.DateField('date_pub')
    tags = TaggableManager()

    def __str__(self):

        return "{0}".format(self.id)


def post(request, index):
    try:
        page_post = Post.objects.get(id=index)
        previous_id = page_post.id - 1
        next_id = page_post.id + 1
        tag = page_post.tags
        context = {
            'page_post': page_post,
            'previous_id': previous_id,
            'next_id': next_id,
            'max_id': len(Post.objects.all()),
            'tag': tag,
        }
        return render(request, "post.html", context)
    except Post.DoesNotExist:
        return render(request, "error_page.html")


       <div class="container" style="margin-top: 100px;">

            <div class="row">
                <div class="col-xs-0 col-md-3"></div>
                <div class="col-xs-0 col-md-6">
                    {% for post in posts %}
                    <div class="post-block">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-xs-12">
                                    <h3>{{ post.title|truncatechars_html:30 }}</h3>
                                    <h6>Теги: {{ post.tag }}</h6>
                                    <h4 class="text-right">Дата: {{ post.date_pub }}</h4>
                                    <hr>
                                </div>
                            </div>

                        </div>
                        <div class="row">
                            <div class="col-xs-12">
                                <div class="col-xs-11">
                                    <p class="col_for_main">{{ post.text|truncatechars_html:100 }}</p>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-xs-12 text-right">
                                <div class="btn-group">
                                    <a class="btn btn-sm btn-default" href="post/{{ post.id }}">Read</a>
                                    <button class="btn btn-sm btn-default">Add to favorite</button>
                                </div>
                            </div>
                        </div>
                    </div>
                        <br>
                    {% endfor %}
                </div>
                <div class="col-xs-0 col-md-3"></div>
            </div>
        </div>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Astrohas, 2017-09-15
@Astrohas

{{ post.tag}}

Post has no such property

T
tema_sun, 2017-09-15
@tema_sun

But why should this work at all? You don't pass posts to the context. And you don't have to do it this way , there is https://docs.djangoproject.com/en/1.11/ref/models/... ps - this is a bad choice for a function name. Django has built-in tools with the same name. It took me a while to realize that you are not using CBV.
def post(request, index):

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question