I am trying to set up a model to use UUIDs instead of incrementing primary keys in the url. When I use get_absolute_url in the list view I get an error that the reverse path cannot be found, that it is not a valid view function or pattern name.
Reverse for '/games/fd7fe4df-71ef-4b39-932e-f09e0707c925' not found. '/games/fd7fe4df-71ef-4b39-932e-f09e0707c925' is not a valid view function or pattern name.
However, if I paste that path into the url bar, like http://localhost:8000/games/fd7fe4df-71ef-4b39-932e-f09e0707c925, then I can go to the detail view of the model perfectly fine. I am not sure what I am missing here.
My urls.py
...
urlpatterns = [
path("", GameListView.as_view(), name="game_list"),
path("<uuid:pk>", GameDetailView.as_view(), name="game_detail"),
]
The models.py
...
class Game(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False
)
...
def get_absolute_url(self):
return reverse("game_detail", kwargs={"pk": self.pk})
The views.py
...
class GameListView(ListView):
model = Game
context_object_name = "game_list"
template_name = "games/game_list.html"
class GameDetailView(DetailView):
model = Game
context_object_name = "game"
template_name = "games/game_detail.html"
The relevant html in the list view
{% for g in game_list %}
<a href="{% url g.get_absolute_url %}"