Skip to content

Commit

Permalink
Prefer slugs over ids
Browse files Browse the repository at this point in the history
  • Loading branch information
anze3db committed Nov 2, 2024
1 parent e583c7b commit 6c3933e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions surfcamsapi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.contrib import admin
from django.db.models import Prefetch
from django.shortcuts import render
Expand All @@ -24,8 +25,14 @@
from surfline.urls import get_surfline_data


async def get_full_detail(request, cam_id: int):
cam = await Cam.objects.aget(id=cam_id)
async def get_full_detail(request, cam_id: str):
try:
cam = await Cam.objects.aget(slug=cam_id)
except Cam.DoesNotExist:
if cam_id.isdigit():
cam = await Cam.objects.aget(id=cam_id)
else:
raise
related_cams = await cam.related_cams()

return render(
Expand All @@ -52,7 +59,7 @@ async def cams(request):

urlpatterns = [
path("", cams, name="cams"),
path("cams/<int:cam_id>/", get_full_detail, name="cam_full_detail"),
path("cams/<str:cam_id>/", get_full_detail, name="cam_full_detail"),
path("surfline/<int:cam_id>/", get_surfline_data, name="surfline_detail"),
path("admin/", admin.site.urls),
path("api/index", cams), # TODO: remove soon
Expand Down
2 changes: 1 addition & 1 deletion templates/cams.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h3 style="color: {{ category.color }}">{{ category.title }}</h3>
<li style="list-style: none;">
<span style="color: {{ cam.bullet_color }}"></span>
<a style="color: {{ cam.title_color }}"
href="{% url 'cam_full_detail' cam.id %}">{{ cam.title }}</a>
href="{% url 'cam_full_detail' cam.slug %}">{{ cam.title }}</a>
</li>
{% endfor %}
</ul>
Expand Down

0 comments on commit 6c3933e

Please sign in to comment.