redirect to previous page after a successful edit?

Basically, I want to implement in code what happens when the user presses the back button in a browser.

The main reason is that the edit form for a particular model can be accessed via different url routes, and therefore there are multiple access points into the edit form. I would prefer not having to hard code all the various access points if that makes any sense.

History.back() can emulate the browser’s back button

Thanks buddy, the more I think about it, I should know how users are linking through to various models. Therefore, what I’ve currently done is examine in the model the type of user and how they ended up there. I know an ordinary user won’t come through to the model if they aren’t a super-user via a list of users (if that makes any sense). At the moment, I allow super-users to view all other users without using django admin (I don’t know if that’s a good/bad idea – but I’m doing it to learn something) . btw - I bunged in city to see if it worked as descendant of user (I would probably link it to a different model (i.e. address) in the real world.

class MyUser(AbstractUser):
    City = models.TextField(default='')
    def get_absolute_url(self):
         if self.is_superuser:
            return reverse('users')  
            return reverse('index')

If it serves your purpose it works.
Surely you can optimize it but, as you say, you’re learning so in the future you may know why this is a good/bad option based on experience.

A common pattern (and one that is used by the Django admin site) is to use something like a next querystring parameter. The view code can detect if the next parameter exists and redirect to that page if it does.

Here’s an example from one of my projects where I applied this pattern.

<a href="{% url "courses:task_edit" uuid=task.uuid %}?next={{ request.path|urlencode }}">
1 Like

Ah…yes. Like it.

I think I read somewhere, maybe two-scoops django, that you should keep urls simple and let the views do the heavy lifting. I’m probably way of track though, as I’ve read a lot of stuff recently and I’m probably totally wrong. It’s all sort of coalesced and needs to be filtered.

Do you have a github account I can view your code?

I do! The repo for this example is I stream about the project every week on Twitch so I post videos to YouTube from each stream. I’ve got extra info on the project at I hope that helps.


Thanks dude, it does, I’ll be sure to have a look at the repo.