In your case the list comprehensions probably made it more readable. In other cases list comprehensions can make things less readable (I'm guilty of doing this sometimes). I thought maybe you were using brackets as I did that too the first time I wrote a template.
Dave On Jan 10, 2008 11:28 AM, Matic Žgur <[EMAIL PROTECTED]> wrote: > > Thanks for your suggestions. I have managed to get rid of my > get_entries_list() and pass queryset directly to the template. I made > the mistake of using {{ entry.get_absolute_path() }} instead of {{ > entry.get_absolute_path }} in the templates. I've also rewritten a lot > of for loops into list comprehensions (I guess I still have some > habits from PHP). > > Cheers, > Matic Žgur > > On Jan 10, 2008 6:04 PM, David Grant <[EMAIL PROTECTED]> wrote: > > I think you should be able to access the query sets directly in your > > template. Not sure why that didn't work for you. > > > > Here they pass a queryset to render_to_response: > > > http://www.djangoproject.com/documentation/tutorial03/#a-shortcut-render-to-response > > > > Put a loop in your template and then access attributes of the loop > variable > > and you should be good. Here's another example: > > > http://www.djangoproject.com/documentation/tutorial03/#write-views-that-actually-do-something > > They pass latest_poll_list to the template and then look at the template > > where they have: > > > > {% if latest_poll_list %} > > <ul> > > {% for poll in latest_poll_list %} > > <li>{{ poll.question }}</li> > > {% endfor %} > > </ul> > > {% else %} > > > > <p>No polls are available.</p> > > {% endif %} > > > > You should be able to get rid of your get_tags and get_all_entries > > functions, or at the very least replace them with something that returns > a > > queryset rather than constructing a list of dictionaries. > > > > Dave > > > > > > > > On Jan 10, 2008 4:12 AM, Matic Žgur <[EMAIL PROTECTED]> wrote: > > > > > > Hi everybody, > > > > > > being new in Django, I'd like to ask you for some tips about what > > > should be in views.py and what shouldn't be. > > > > > > I'm writing a blog app with custom views.py. I know that it would be a > > > lot easier (and wiser) to use generic views for what I'm trying to > > > accomplish, but I'd like to learn new things along the way, so I > > > decided to write my own views.py. Anyway, that's what bugs me: > > > > > > def get_all_tags(): > > > > > > tags = [] > > > tags_db = Tag.objects.all() > > > > > > for tag in tags_db: > > > tags.append({ > > > 'tag': tag.tag, > > > 'url': tag.get_absolute_url () > > > }) > > > > > > return tags > > > > > > def get_entries_list(entries_list): > > > > > > entries = [] > > > > > > for entry in entries_list: > > > entries.append({ > > > 'body': mark_safe(entry.body ), > > > 'pub_date': entry.pub_date, > > > 'title': entry.title, > > > 'url': entry.get_absolute_url(), > > > 'tags': entry.get_tags(), > > > }) > > > > > > return entries > > > > > > def show_entries_by_day(request, year, month, day): > > > now = datetime.datetime.now() > > > > > > # Check if day is in the future > > > if year + month + day > ''.join([str( now.year), str(now.month), > > > str(now.day)]): > > > raise Http404 > > > > > > entries = Entry.objects.filter(pub_date__lte=now, > > > pub_date__year=year, > > > pub_date__month=month, > > > pub_date__day=day, > > > status=1) > > > > > > return render_to_response('blog_entry.html', {'entries': > > > get_entries_list(entries), 'tags': get_all_tags()}) > > > > > > I have a function similar to show_entries_by_year() for month, day, > > > tag and slug. Now, for get_all_tags(), I'm planning to write a > > > template tag that would replace this function, but I still don't know > > > what to do with get_entries_list(). I only made this function in order > > > to not repeat myself. Is there a way to get rid of get_entries_list() > > > and somehow make my views.py cleaner? I tried to pass entries directly > > > to the template but it didn't quite work (maybe I'm missing > > > something). > > > > > > I searched the web for some examples, but it's hard to find apps that > > > don't use generic views (at least I didn't find them), so I decided to > > > ask here. > > > > > > Thanks > > > Matic Žgur > > > > > > > > > > > > > > > > > > > > -- > > David Grant > > http://www.davidgrant.ca > > > > > > > > > > > > > -- David Grant http://www.davidgrant.ca --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---