How do I get a Django redirect to work properly when updating my database?

I can't get my redirect to work correctly after updating a bicycle in my Django app.

lines of code: highlighted code line 12 is bikes/int:bike_id/ and the error message below says bikes/int:bike_id/ didn't match any of the potential urls.

When I use my update_bike view and update the bicycle profile, if my redirect is just to '/bikes', I'm taken back to the bicycle collection/bicycles owned with no issue. But when I try and redirect back to the profile of the bike I just editted, I get this error that my return pathway doesn't match any of the url patterns. Even though that's the same pathway that initially takes me to the bicycle profile page (not the same page where I edit the bicycle, that is a different page).

I've pasted the function code here:

def bikes_update(request, bike_id):
    if request.method != 'POST' or 'user_id' not in request.session:
        return redirect("/") 
    this_bike = Bike.objects.filter(id=bike_id).update(
        name = request.POST['bike_name'],
        model = request.POST['model_id'],
    )
    return redirect('/bikes')
    return redirect('/bikes/<int:bike_id>')
    return redirect('/bikes/<int:bike_id>/')

I obviously only use one redirect not all the three in the final block. But I can't figure out why at least one of the final two don't work. When I have the bottom two redirects commented out, the return redirect('/bikes') works fine, but it takes me to the page listing all of my bikes. I want to go back the profile of the bike I just editted so I don't have to navigate back to it from the bicycle collection. Navigating to /bikes/int:bike_id/ works fine when going to the profile initially, but after I finishing editing is when I run into trouble even thought it's the same line. As soon as I click the submit button and try to redirect back to /bikes/int:bike_id/ I get my error.

I'm able to update the bicycle profile correctly, so it's something to do with this redirect, but I don't get it.

I don't understand how line 12 in my screen snippet and the current path (both highlighted in the image), aren't the same/don't match.

If you want to redirect on /bikes/<int:bike_id>/ then you can use a format string like this...

def bikes_update(request, bike_id):
  if request.method != 'POST' or 'user_id' not in request.session:
      return redirect("/")
  this_bike = Bike.objects.filter(id=bike_id).update(
      name = request.POST['bike_name'],
      model = request.POST['model_id'],
  )
  return redirect(f'/bikes/{bike_id}/')

You have to add the id here not this "int:bike_id". put your id variable here like this redirect(f'/bikes/{Id}').

redirect can take a model as an argument, so you could do something like:

this_bike = get_object_or_404(Bike, pk=bike_id)    
return redirect(this_bike)

You will need a get_absolute_url method in your models.py though. For example:

def get_absolute_url(self):
    return reverse("bikes", kwargs={"pk": self.pk})

This can be used for other things in Django as well (including usage in templates).

More info here: Django Docs

Back to Top