[Answered ]-IndexError: string index out of range – Django – Why?

1👍

Why not just do:

from django.shortcuts import render, get_object_or_404
from your_app.models import Location

def get_location(request, lid):
    location = get_object_or_404(Location, id=lid)
    return render(request, 'location.html', {'location': location})

The reason the DoesNotExist exception is being thrown is because the id you’re looking for doesn’t exist in the database being queried as @hellsgate mentioned.

1👍

The error is actually occurring in the first line of your try: block

location = Location.objects.get(id=lid).

This then triggers the Location.DoesNotExist exception. The reason for this is that the location id being used in the .get doesn’t exist in the database location table. Make sure your production database contains the same location data as your development database, including the IDs, and this error will disappear.

Leave a comment