0

Django 1.7

I have a model:

class Book(models.Model):
    author = models.CharField(
        'Автор',
        max_length = 200, 
        blank = True
    )
    title = models.CharField(
        'Заглавие',
        max_length = 200
    )
    pub_year = models.IntegerField(
        'Год издания', 
        max_length = 4, 
        choices = YEAR_CHOICES
    )

In my views.py I have:

from django.shortcuts import render, redirect, get_object_or_404
from django.views import generic
from books.models import Book
from forms import BookForm

class BookListView(generic.ListView):

    model = Book
    paginate_by = 25

I want to get verbose_names of Book model fields in template. In order to do it I have registered template tag (function code is from https://stackoverflow.com/a/2429094/2947812):

@register.filter
def verbose_name(value, arg): 
    return value._meta.get_field_by_name(arg)[0].verbose_name 

and in template I use it this way:

{{ book|verbose_name:"title" }}

As I can tell, everything is made according to Django documentation on custom template tags and filters.

But instead of verbose_name value, I get 'str' object has no attribute '_meta' error. I guess that it's because instead of Book object, something other is passed to verbose_name function, but why?

Community
  • 1
  • 1
Mikhail Batcer
  • 1,938
  • 7
  • 37
  • 57
  • show your view code please – user2717954 Nov 13 '14 at 16:01
  • @user2717954 There is `views.py` code in the post. – Mikhail Batcer Nov 13 '14 at 16:05
  • 1
    sorry i didnt know about ListView until this very moment but from reading the docs it seems you need to do {% for book in object_list %} {{ book|verbose_name:"title" }} {% endfor %}. is this how you try to access the variable book? – user2717954 Nov 13 '14 at 16:09
  • Well, I changed `book` to `book_list.0` and now it's OK. But if there will be no items in book_list, then I'll get another error... – Mikhail Batcer Nov 13 '14 at 18:28
  • Is this question still open? I have a similar question but your specifics seem inconsistent. Can you answer your own question or reformat this in some way that is more meaningful? – Wade Williams Dec 11 '14 at 01:11
  • @WadeWilliams This particular question was only because of me being inattentive to my template's code, and it's answered by user2717954's comment. But what I want to accomplish in general is to get model's `verbose_name` from template, and I still don't know how to do it universally. – Mikhail Batcer Dec 11 '14 at 09:15
  • @WadeWilliams I've added an answer to this post to be more consistent. – Mikhail Batcer Dec 11 '14 at 09:39

1 Answers1

0

I mistakenly used {{ book|verbose_name:"title" }} without having variable book set. My template is for ListView, and book is set only within {% for book in book_list %} ... {% endfor %} loop. So I rewrote my template this way:

<table class="table table-striped">
    <thead>
        <tr>
            <th>ID</th>
            <th>{{ book_list.0|verbose_name:"author" }}</th>
            <th>{{ book_list.0|verbose_name:"title" }}</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        {% for book in book_list %}
            <tr>
                <td>{{ book.id }}</td>
                <td>{{ book.author }}</td>
                <td>{{ book.title }}</td>
            </tr>
        {% endfor %}
    </tbody>
</table>
Mikhail Batcer
  • 1,938
  • 7
  • 37
  • 57