[pyar] [semi OT] Django generic views y forms

Roberto Alsina ralsina en netmanagers.com.ar
Mie Jul 14 21:10:29 ART 2010


On Wednesday 14 July 2010 20:52:54 Hernan Olivera wrote:
> El 14 de julio de 2010 20:31, Roberto Alsina
> 
> <ralsina en netmanagers.com.ar>escribió:
> > On Wednesday 14 July 2010 20:22:18 Hernan Olivera wrote:
> > > Estimados:
> > > 
> > > Estoy tratando de usar las generic views de Django para realmente hacer
> > > DRY, y me encuentro con un problema. Segun la documentacion (
> > 
> > http://docs.djangoproject.com/en/dev/ref/generic-views/#ref-generic-views
> > ),
> > 
> > > el context tiene un form que se puede usar en el template, pero el
> > 
> > template
> > 
> > > no 've' el form, y no encuentro forma de pasarselo, ni en el
> > > diccionario, ni de ninguna otra forma.
> > > 
> > > Obviamente estoy usando las generic para escribir menos codigo, y
> > 
> > quisiera
> > 
> > > no tener que escribir campo a campo el template, sino usar el form (que
> > > a su vez derivo del model)
> > 
> > No entiendo el problema. Podés usar {{form.as_table}} o lo que quieras,
> > no?
> 
> eso es lo que quiero hacer, pero no funciona porque no llega el form al
> template y por eso no funciona.
> 
> cuenta_info = {
>     "queryset": Cuenta.objects.all(),
> }
> 
> y despues en urlpatterns:
> 
> (r'^ver/cuenta/(?P<object_id>\d+)', list_detail.object_detail, cuenta_info,
> )
> 
> En los casos de object_create y object_modify, no tiene problema.
> Lo que no logro es sencillamente mostrar una instancia, usando
> form.as_table o similar en este caso. Y me parece un embole tener que
> escribir uno a uno los campos object.nombre, object.direccion...
> 
> ¿o se me esta escapando alguna tortuga?

Que object_detail es un view genérico sin form :-)

No es para editar/modificar/crear, es sólo para mostrar, así que ¿que form vas 
a tener?

django.views.generic.list_detail.object_detail
A page representing an individual object.

Description:

A page representing an individual object.

Required arguments:

queryset: A QuerySet that contains the object.

Either object_id or (slug and slug_field) is required.

If you provide object_id, it should be the value of the primary-key field for 
the object being displayed on this page.

Otherwise, slug should be the slug of the given object, and slug_field should 
be the name of the slug field in the QuerySet's model. By default, slug_field 
is 'slug'.

Optional arguments:

template_name: The full name of a template to use in rendering the page. This 
lets you override the default template name (see below).

template_name_field: The name of a field on the object whose value is the 
template name to use. This lets you store template names in the data. In other 
words, if your object has a field 'the_template' that contains a string 
'foo.html', and you set template_name_field to 'the_template', then the 
generic view for this object will use the template 'foo.html'.

It's a bit of a brain-bender, but it's useful in some cases.

template_loader: The template loader to use when loading the template. By 
default, it's django.template.loader.

extra_context: A dictionary of values to add to the template context. By 
default, this is an empty dictionary. If a value in the dictionary is 
callable, the generic view will call it just before rendering the template.

context_processors: A list of template-context processors to apply to the 
view's template.

template_object_name: Designates the name of the template variable to use in 
the template context. By default, this is 'object'.

mimetype: The MIME type to use for the resulting document. Defaults to the 
value of the DEFAULT_CONTENT_TYPE setting.

Template name:

If template_name isn't specified, this view will use the template 
<app_label>/<model_name>_detail.html by default.

Template context:

In addition to extra_context, the template's context will be:

object: The object. This variable's name depends on the template_object_name 
parameter, which is 'object' by default. If template_object_name is 'foo', 
this variable's name will be foo.



More information about the pyar mailing list