Hi,
I have a model called ‘Building’, which has a UUID primary key. The models instances also have a foreign key called ‘scheme’. And I would like to have the URL for the detail view as follows:
‘’’
urlpatterns += [path(‘scheme/uuid:pk1/building/uuid:pk2/’, comp_view.BuildingDetailView.as_view(), name=‘building-detail’),]
‘’’
And my model is as follows:
‘’’
class Building(models.Model):
“”“Model representing a building instance of a specific scheme.”“”
id = models.UUIDField(
primary_key=True, default=uuid.uuid4, editable=False,
help_text="Unique ID for the building instance",
)
scheme = models.ForeignKey(
WaterScheme, on_delete=models.CASCADE,
related_name='buildings'
)
building_type = models.ForeignKey(
BuildingType, on_delete=models.RESTRICT,
)
name = models.CharField("Building name", max_length=128)
updated_by = models.ForeignKey(
settings.AUTH_USER_MODEL, related_name='buildings',
on_delete=models.SET_NULL, blank=True, null=True
)
updated_on = models.DateTimeField(auto_now=True)
def __str__(self) -> str:
return f"{self.scheme.name.split(' ', 1)[0]} {self.name}"
def get_absolute_url(self):
return reverse('schemes:building-detail', kwargs={'pk1': scheme.id, 'pk2': scheme.id})
class Meta:
ordering = ['name', 'id',]
‘’’
I’ve tried several variations with the ‘kwargs’ of the get_absolute_url in the above model, but whenever I plug it into a template, I get this error:
Invalid block tag on line 14: ‘building.get_aboslute_url’, expected ‘empty’ or ‘endfor’. Did you forget to register or load this tag?
And the template is this:
‘’’
{% block content %}
<h3>Scheme buildings</h3>
<ul>
{% for building in building_list %}
<li>
<a href="{% building.get_aboslute_url %} ">{{ building.name }}</a>
({{ building.building_type }})
</li>
{% empty %}
<li>No buildings have been added...</li>
{% endfor %}
</ul>
{% endblock %}
‘’’
Am I doing something wrong with the get_absolute_url by trying to pass two variables? Or does the issue lies elsewhere? Any help on the matter is much appreciated.
(sorry about the weird formatting… i can’t figure out how to set the start and end of code blocks)