I’m struggling with get_absolute_url(). I’m unsure what’s causing issues since elsewhere in another template the get_absolute_url() is functioning fine (it works in a for loop but not in a single href link). Probably also useful to note that there aren’t any errors when clicking, it’s just not loading the detail page of the person logged in. I thought since it’s not adding any uuid in the url when I click the link, it might have something to do with passing in the id somewhere in the template, however since the id is already being passed to the url in the models file and successfully works in other places I’m “thrown for a loop” (so to speak) as to what I’ve done wrong.
Help appreciated, and code excerpts below:
MODELS excerpt
class Profile(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False)
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
date_modified = models.DateTimeField(CustomUser, auto_now=True)
def get_absolute_url(self):
return reverse("profile_detail", args=[str(self.id)])
URLS excerpt
from django.urls import path
from .views import ProfileListView, ProfileDetailView
urlpatterns = [
path("", ProfileListView.as_view(), name="profile_list"),
path("<uuid:pk>", ProfileDetailView.as_view(), name="profile_detail"),
]
TEMPLATE THAT DOESN’T WORK excerpt
(note: I replicated the link in a separate static page just so I could focus on this alone before trying to add it anywhere more complicated)
<h1>Static Page</h1>
<a href="{{ profile.get_absolute_url }}">My Profile</a>
TEMPLATE THAT ALREADY WORKS excerpt
{% for profile in profile_list %}
<div class="card mb-3" style="max-width: 540px;">
<div class="row g-0">
<div class="col-md-4">
<img src="{% static 'images/testimage.jpg' %}" class="img-fluid rounded-start" alt="Profile Image">
</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title">{{ profile.user.username }}</h5>
<p class="card-text"><a href="{{ profile.get_absolute_url }}">{{ profile.user.username }}</a></p>
<br />
<p class="card-text"><small class="text-body-secondary">{{ profile.date_modified }}</small></p>
</div>
</div>
</div>
</div>
{% endfor %}
Thanks in advance!
