1

I'm using the django(v.1.2) generic view "list_detail" to print some objects (Model Article) stored in a db.

In urls.py I added the following line

urlpatterns += patterns('django.views.generic.list_detail',
url(r'^article/(?P<slug>[\-\d\w]+)/$', 'object_detail', {'slug_field': 'title_slug', 'queryset': Article.objects.filter(is_public=True)}, name='article'),

)

In the respective template (article_detail.html) I would like to print the article iterating over all its fields. Actually I wrote:

{% for k,v in object.fields %}
  <p>{{k}}:{{v}}<p>
{% endfor %}

but it doesn't work. Any suggestions?

Sebastien
  • 11
  • 1
  • 2
  • The answer is [here](http://stackoverflow.com/questions/2170228/django-iterate-over-model-instance-field-names-and-values-in-template) – DrTyrsa May 26 '11 at 09:48
  • what is object in object.fields? – Narendra Kamma May 26 '11 at 10:30
  • @Narendra Kamma "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". [docs](https://docs.djangoproject.com/en/dev/ref/generic-views/#django-views-generic-date-based-object-detail) – DrTyrsa May 26 '11 at 10:53
  • I understand that, what is object in your template. you are not sending anything to template with name 'object'. You are sending queryset of Article, which will be a list. – Narendra Kamma May 26 '11 at 10:56
  • @Narendra Kamma: In addition to extra_context, the "object" is sent to the template's context. You don't need to send it to the template because it is in 'object_detail' view. But you can read this in the django docs as linked before by DrTyrsa. – Sebastien May 26 '11 at 11:39
  • @DrTyrsa: I read the 3D that you linked in your first comment, but I cannot find a suitable solution for my case. All the proposed solutions need new methods for the Model class (I don't want to do this because I generate my models.py automatically using "inspectdb") or modify the view (I cannot do this, it is a generic django view). Just to be clear, I just would like to know if there is a way to iterate a model "field name/value" in a template without change anything on the other files. – Sebastien May 26 '11 at 11:55
  • @Sebastien You can write your own template tag for that. I don't think it's possible to solve your task using only django built-in template language. – DrTyrsa May 26 '11 at 12:02

2 Answers2

0

Let us solve this by an example-

I have a model as-

class item(models.Model):
    item_number = models.AutoField(primary_key=True)
    item_name = models.CharField(max_length=200)
    lifetime_rating = models.DecimalField(max_digits=3,decimal_places=1, default=0)
    current_rating = models.DecimalField(max_digits=3,decimal_places=1, default=0)
    lifefeedbacknum = models.IntegerField(default=0)
    currentfeedbacknum = models.IntegerField(default=0)

    def __unicode__(self):
        return self.item_name

    def retlifetime_rating(self):
        return self.lifetime_rating

Note method-retlifetime_rating which returns the value of lifetimerating for an object instance.

Now we wish to display lifetime ratings for all products

in views.py-

def showrating(request):
    itemslist= models.item.objects.all()
    return render(request, 'showrating.html', 'sitems' : itemslist)

the showrating.html file contains the following code snippet-

{% for element in sitems %}
        <tr>
            <td>{{ element }}</td>
            <td>{{ element.retlifetime_rating }}</td>
        </tr>
    {% endfor %}

basically if you wish to display different fields of an object, you need to have a corresponding method call to return that field

off-course there are other ways to do it but this is most likely the simplest and easiest to implement

0

'queryset': Article.objects.filter(is_public=True) sends a list to your template. but in template you are treating it like an Article object.

'article': Article.objects.filter(is_public=True)[0]

then you can access all items of Article. However, I don't understand what you are trying to do with it. is Article.fields a list or dict?

Narendra Kamma
  • 1,431
  • 1
  • 11
  • 19