On Oct 22, 5:10 pm, Ardesco <[EMAIL PROTECTED]> wrote:
> Taking an example from Chapter 7 of the Django Book:
>
> from forms import PublisherForm
>
> def add_publisher(request):
>     if request.method == 'POST':
>         form = PublisherForm(request.POST)
>         if form.is_valid():
>             form.save()
>             return HttpResponseRedirect('/add_publisher/thanks/')
>     else:
>         form = PublisherForm()
>     return render_to_response('books/add_publisher.html', {'form':
> form})
>
> If say my model had 10 attributes, and I wanted to split it up into 5
> different blocks (i.e. put pairs of elements in their own div block
> and whatnot), is there any easy way to do this? I know if you pass
> lists in, you can do something along the lines of:
> {% for item in form|slice:":2" %} to get the first two elements, for
> example, but you apparently cannot do this when you pass a form object
> to the template. Is there an easy way to do this without writing a lot
> of code in the template/view?

Easy. Turn the fields in the a list and then slice works. But you have
to do that in the view as far as a I know.
So

fields = list(my_form)
return render_to_response(...

Then:

  {% for field in fields|slice:":5" %}
    {{ field }}
  {% endfor %}


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to