0

I can't get access to only Django Translated fields in Template, it's displayed whenever if it returns Null in Template. Here is example of my template code:

{% for book in books %}
        <div class="item">
            <a href="/{{ lang }}/{% get_attr book 'file_' lang %}" title="{% get_attr book 'title_' lang %}">
                <img src="/media/{% get_attr book 'file_preview_' lang %}"/>
            </a>
        </div>
    {% empty %}
        <h3>Empty</h3>
    {% endfor %}

And here is my view code example:

class BooksView(TemplateView): template_name = "library/page/library_page.html" model = Book

def get_context_data(self, **kwargs):
    context = super(BooksView, self).get_context_data(**kwargs)
    context.update(modules.standard(self.request, kwargs))
    context['books'] = Book.objects.filter(published=True, )

    return context

It's shown like this: Error pictures

Here is model:

class Book(models.Model):
    title = models.CharField(max_length=100)
    file = models.FileField(upload_to='library/file/%Y/%m/%d')
    file_preview = models.FileField(upload_to='library/file_preview/%Y/%m/%d')
    publishedTime = UnixDateTimeField(auto_now=True)
    published = models.BooleanField(default=True)


    def __unicode__(self):
        return self.title

In django shell , after four null objects it show title , i don't want to display NULL objects, and above i uploaded Image which can't find image urls, therefor i need to check in template for null object or view i must add correct filter for modeltranslation

>>> l = Book.objects.order_by('pk')
>>> l
[<Book: Book 1 en>, <Book: Second book en>, <Book: Third book 3>, <Book: Fourth book en>, <Book: Fifth book en>]
>>> for x in l:
...     print x.pk,"-",x.title_ru
... 
1 - 
2 - 
3 - 
4 - 
5 - Pyataya kniga ru


<div class="books">
    <div class="item">
      <a href="/ru/" title="">
        <img src="/media/">
      </a>
    </div>
    <div class="item">
      <a href="/ru/" title="">
        <img src="/media/">
      </a>
   </div>
   <div class="item">
      <a href="/ru/" title="">
         <img src="/media/">
      </a>
   </div>
   <div class="item">
      <a href="/ru/" title="">
         <img src="/media/">
      </a>
   </div>
   <div class="item">
      <a href="/ru/library/file/2016/04/08/Computer_Glossary.pdf" title="Pyataya kniga ru">
        <img src="/media/library/file_preview/2016/04/08/pdf_2.jpg" style="
width: 100px;
height: 100px; ">
      </a>
   </div>
 </div>
Uri
  • 2,992
  • 8
  • 43
  • 86
A'zam Mamatmurodov
  • 370
  • 1
  • 5
  • 14
  • Some indentation is missing in your class BooksView. Could you correct it please? Could you also add a sample of rendered html code and check if there are these attributes you're looking for? isn't it only that the picture isn't found? – vmonteco Apr 09 '16 at 13:14
  • What certian is missing in Class ? Can you give some cheat sheet for fixing ? – A'zam Mamatmurodov Apr 09 '16 at 14:06
  • The indentation you use in the **BooksView** class is incorrect. And you don't provide enough elements to make us understand what the problem is. – vmonteco Apr 09 '16 at 14:10
  • ok , let's say , We have a Book model which fields are translated using django modeltranslation, and I created some Book model contents , and there are 2 languages in site , and some contents in english and some contents in russian , and in English page i want to display english contents , and also in russian is same . – A'zam Mamatmurodov Apr 09 '16 at 14:23
  • But could you provide some rendered html code please? – vmonteco Apr 09 '16 at 14:28
  • ok , let's say , We have a Book model which fields are translated using django modeltranslation, and I created some Book model contents , and there are 2 languages in site , and some contents in english and some contents in russian , and in English page i want to display english contents , and also in russian is same . – A'zam Mamatmurodov Apr 09 '16 at 14:30
  • Could you rather provide the code by editing your post **AND** using markdown to make it readable? – vmonteco Apr 09 '16 at 14:33
  • Add the **HTML** it renders please. – vmonteco Apr 09 '16 at 14:47
  • I added HTMl , it changes something ? – A'zam Mamatmurodov Apr 09 '16 at 15:22

0 Answers0