Django Single Listing, 404, Attributes from Another Table Django.How

Author avatar wrote on 26/05/2022

404

We want to show a 404 page if user type a URL of a non-exisiting listing, for example, we have only 5 listings but the requested URL is listing/10 which does not exist.

in views.py


from django.shortcuts import get_object_or_404, render
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from .models import Listing



def listing(request, listing_id):
    listing = get_object_or_404(Listing, pk=listing_id)
    context = {
        'listing' : listing
    }
    return render(request, 'listings/listing.html', context)
 

Accessing data of another model using its foreign key

In the listing model we passed we have realtor as a foreign key, we do not need to pass it model to access its properties we just use {{listing.realtor.photo.url}}