Re: Trying to display a list
On Sun, 2008-01-06 at 12:38 -0800, chiefmoamba wrote: > I'm sure this will be very easy for most of you... > > I am trying to display a list. [...] > If I view source, I get this: > > There are books > The title of your books: [, ] That's the default representation of a Python list: using the repr() of the elements inside the list. The reason it isn't showing up in the rendered page is because angle brackets are used and the browser interprets them as HTML tags. Using, for example, the join filter, as Alex pointed out, will send each instance through it's str() and give you want you are after. Regards, Malcolm -- Save the whales. Collect the whole set. http://www.pointy-stick.com/blog/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Trying to display a list
{% if "book_list" %} - what is "book_list" ? It is string literal, so it is always True. You must write this: {% if book %} And if you want to print books: {{book|join:", "}} On 6 янв, 23:38, chiefmoamba <[EMAIL PROTECTED]> wrote: > I'm sure this will be very easy for most of you... > > I am trying to display a list. > > in views.py, I have: > > from mysite.books.models import Book > from django.http import HttpResponse > > def book_list(request): > book = Book.objects.all().order_by('title') > return render_to_response('books/book_list.html', {'book': book}) > > in models.py, I have: > > class Book(models.Model): > title = models.CharField(maxlength=100) > authors = models.ManyToManyField(Author) > publisher = models.ForeignKey(Publisher) > publication_date = models.DateField() > num_pages = models.IntegerField(blank=True, null=True) > > def __str__(self): > return self.title > > In my HTML is: > > > {% if "book_list" %} > There are books > {% endif %} > > > > The title of your books: {{ book }} > > > Now the thing is, when it renders in the browser, I get something that > looks like this: > > There are books > The title of your books: [, ] > > If I view source, I get this: > > There are books > The title of your books: [, ] > > So clearly the data is there - I just can't see it. Can anyone help > shed light on this, please? I have been at it all day and I am am > about to crack! ;-) > > (Incidentally, this relates to some code at the start of Chapter 5, > djangobook) > > Thanks, > > Ed --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Trying to display a list
I'm sure this will be very easy for most of you... I am trying to display a list. in views.py, I have: from mysite.books.models import Book from django.http import HttpResponse def book_list(request): book = Book.objects.all().order_by('title') return render_to_response('books/book_list.html', {'book': book}) in models.py, I have: class Book(models.Model): title = models.CharField(maxlength=100) authors = models.ManyToManyField(Author) publisher = models.ForeignKey(Publisher) publication_date = models.DateField() num_pages = models.IntegerField(blank=True, null=True) def __str__(self): return self.title In my HTML is: {% if "book_list" %} There are books {% endif %} The title of your books: {{ book }} Now the thing is, when it renders in the browser, I get something that looks like this: There are books The title of your books: [, ] If I view source, I get this: There are books The title of your books: [, ] So clearly the data is there - I just can't see it. Can anyone help shed light on this, please? I have been at it all day and I am am about to crack! ;-) (Incidentally, this relates to some code at the start of Chapter 5, djangobook) Thanks, Ed --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---