Created
May 22, 2020 09:11
-
-
Save spookylukey/63bf5c84e22b2c65979a69bde15601a1 to your computer and use it in GitHub Desktop.
get_object_or_404 example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def product_detail(request, slug): | |
try: | |
product = Product.objects.get(slug=slug) | |
except Product.DoesNotExist: | |
raise Http404() | |
return TemplateResponse(request, 'products/product_detail.html', { | |
'product': product, | |
}) | |
# Shorter version | |
def product_detail(request, slug): | |
return TemplateResponse(request, 'products/product_detail.html', { | |
'product': get_object_or_404(Product.objects.all(), slug=slug), | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment