Allow to filter quotes by author slug

This commit is contained in:
Pedro Rey Anca 2024-12-03 13:26:53 +01:00
parent beec22f40a
commit 9a66ba0314
Signed by: peprolinbot
GPG key ID: 053EA6E00116533A
2 changed files with 7 additions and 2 deletions

View file

@ -6,5 +6,6 @@ app_name = "lazaro_quotes"
urlpatterns = [ urlpatterns = [
path("", views.show_quote, name="show_quote"), path("", views.show_quote, name="show_quote"),
path("<str:author_slug>", views.show_quote, name="show_quote"),
path("suggest", views.suggest_quote_form, name="suggest_quote_form") path("suggest", views.suggest_quote_form, name="suggest_quote_form")
] ]

View file

@ -5,8 +5,12 @@ from .models import Quote, Suggester
from .forms import SuggestionForm from .forms import SuggestionForm
def show_quote(request): def show_quote(request, author_slug=None):
quote = Quote.objects.order_by('?').first() or None if author_slug is None:
quote = Quote.objects.order_by('?').first()
else:
quote = Quote.objects.filter(
author__slug=author_slug).order_by('?').first()
return render(request, 'lazaro_quotes/show_quote.html', {"quote": quote}) return render(request, 'lazaro_quotes/show_quote.html', {"quote": quote})