Thanks for the feedback.

Yes, the field names are different for each model and that is why I
asked the question.  I had solved the problem with your suggestion #2:
"Create base template and extend that with model specific templates."
But I did not find this to be a very efficient way to solve the
problem since a model specific template is needed for each model and I
was hoping that Django may have an easier solution that I missed in
the documentation.

Con of this approach: If a field is added/deleted from a model, a
change is needed to the template.
Pro: But it is also allows the custom display of individual models.

It would be cool to see a ModelTable class added within Django (I
guess I could always write one myself too!) that would allow table
data to be displayed with field headers and allow columns to be
sortable.


On Sep 12, 9:07 am, "Norman Harman" <[EMAIL PROTECTED]> wrote:
> SnappyDjangoUser wrote:
> > I am a new django user so please excuse my naive question.
>
> > I have a generic view function (see below) which is used to perform a
> > query on one of many tables (depending on the "model" argument) and
> > then render the results to a single template file (object_list.html)
>
> > def item_list(request, model='device'):
> >     # determine which table
> >     TableName = ModelNameDictionary[model]
> >     order_by_field = OrderByFieldDictionary[model]
>
> >     # Get a list of all entires for this object
> >     if order_by_field == '':
> >         latest_object_list = TableName.objects.all()
> >     else:
> >         latest_object_list =
> > TableName.objects.all().order_by(order_by_field)
>
> >     # Get the number of objects.
> >     paginator = Paginator(latest_object_list, 0)
> >     result_count = paginator._get_count()
>
> >     t = loader.get_template('object_list.html')
> >     c = Context({
> >         'ObjectName': NameDictionary[model],
> >         'latest_object_list': latest_object_list,
> >         'result_count': result_count,
> >     })
> >     return HttpResponse(t.render(c))
>
> > I am planning on using a generic template (object_list.html) to print
> > the data in a tabular format.  How can I access the database field
> > names and the data if I don't know which table was queried and is
> > being renderd?  Below is pseudo code from object_list.html that I am
> > looking for help filling in....
>
> > {% if latest_object_list %}
> >     <table>
> >     <tr>
> >         <!-- Print the table colomn headers -->
> >         {% for header in latest_object_list %}
> >             <th>{{ header.field_name }}</th>
> >         {% endfor %}
> >     </tr>
>
> >     <!-- Print the data -->
> >     {% for item in latest_object_list %}
> >         <tr class="{% cycle row1,row2 %}">
> >             {% for column in item %}
> >                 <td>{{ item.column_data }}</td>
> >             {% endfor %}
> >         </tr>
> >     {% endfor %}
>
> >     </table>
> > {% else %}
> >     <p>No {{ ObjectName }} are available.</p>
> > {% endif %}
>
> I assume the field names are different for different models or you
> shouldn't be asking this question...
>
> There are several ways I can think of:
>
> Create methods in models (or do this in view) that return a list of the
> field names you want displayed and another that returns the field's
> values in the same order.  Access them via loops in template.
>
> Create base template and extend that with model specific templates.
> Each of which knows how to display the columns for a particular model.
>
>  >     {% for item in latest_object_list %}
>  >         <tr class="{% cycle row1,row2 %}">
> {% block columns %}{% endblock %}
>  >         </tr>
>  >     {% endfor %}
>
> The model speciific templates would be just the
> {% extend base_template %}
> {% block columns %}
>    <td>{{ item.column_one }}</td>
>    <td>{{ item.column_two }}</td>
>    <td>{{ item.column_etc }}</td>
> {% endblock %}
> part customized for each model.  In the view select the correct template
> like so:
>
>    t = loader.get_template('%s_list.html' % model.__name__)
>
> Create a wrapper aka "interface" or "proxy" that translates a standard
> set of fieldnames into the model specific fieldnames.  So as far as the
> template is concerned there is only one type of "model", the proxy one.
>
> --
> Norman J. Harman Jr.
> Senior Web Specialist, Austin American-Statesman
> ___________________________________________________________________________
> Get off the sidelines and huddle up with the Statesman all season long
> for complete high school, college and pro coverage in print and online!
--~--~---------~--~----~------------~-------~--~----~
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