I have a model which I want to display as a Detail view, I have created a list view that has a link that leads to its detailed view. I dont get any errors but the template doesn't render any of the models detail Link to DetailView
<a href="../ancillaries/{{ Ancillary.id }}" > Product </a>
Model
from django.db import models
from django.core.urlresolvers import reverse
class Ancillary(models.Model):
product_code = models.CharField(max_length=60, null=True)
type = models.CharField(max_length=120, null=True)
product = models.CharField(max_length=120, null=True)
standard = models.CharField(max_length=120, null=True)
measurement = models.CharField(max_length=120, null=True)
brand = models.CharField(max_length=120, null=True)
class Meta:
verbose_name_plural = "Ancillaries"
def get_absolute_url(self):
return reverse('ancillaries')
def __unicode__(self):
return u'%s %s %s %s %s %s %s' % (self.id, self.product_code, self.type,
self.product, self.standard,
self.measurement, self.brand)
View
class AncillaryDetail(DetailView):
model = Ancillary
def get_context_data(self, **kwargs):
context = super(AncillaryDetail, self).get_context_data(**kwargs)
context['ancillary_list'] = Ancillary.objects.all()
return context
Urls
url(r'^ancillaries/(?P<pk>\d+)/', AncillaryDetail.as_view(template_name='ancillary-detail.html')),
Template
{% for ancillary_list in object_list %}
{{ Ancillary.product}}
{{ Ancillary.type }}
{{ Ancillary.brand }}
{{ Ancillary.measurement }}
{% endfor %}
It looks as though you've used the documentation but adapted the ListView
example incorrectly. If you want to display a single model instance then the DetailView
is the correct view.
As @mrkre suggested you should name your URL pattern (although I would use the singular form for the name).
url(r'^ancillaries/(?P<pk>\d+)/', AncillaryDetail.as_view(
template_name='ancillary-detail.html'), name="ancillary_detail")
The view is then simply
class AncillaryDetail(DetailView):
model = Ancillary
In the template ancillary-detail.html
you access the model instance using the default name object
.
{{ object.product}}
{{ object.type }}
{{ object.brand }}
{{ object.measurement }}