Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redirection to url after edit/delete #101

Open
MohamedAnouar opened this issue Apr 9, 2019 · 1 comment
Open

redirection to url after edit/delete #101

MohamedAnouar opened this issue Apr 9, 2019 · 1 comment

Comments

@MohamedAnouar
Copy link

I wanted to change the redirection url after deleting/editing an object I tried changing this but it did not work for some reason. Is there any other easier way? the problem is that I want to redirect to model.attribute.id. and that can't be handled after deleting a model.

def get_delete_view(self):
ODeleteClass = self.get_delete_view_class()

    class ODeleteView(self.mixin, ODeleteClass):
        namespace = self.namespace 
        perms = self.perms['delete']
        all_perms = self.perms
        view_type = 'delete'
        views_available = self.views_available[:]
        check_perms = self.check_perms
        template_father = self.template_father
        template_blocks = self.template_blocks
        related_fields = self.related_fields

        def get_success_url(self):
            url = super(ODeleteView, self).get_success_url()
            print(self.getparams)
            if (self.getparams):  # fixed filter delete action
                url += '?' + self.getparams
            return ("/Project/"+ self.project.id + "/interfacelist")

    return ODeleteView
@telenieko
Copy link
Collaborator

Your code snippet, in your CRUD class should read (note, untested code):

def get_delete_view(self):
    delete_view_class = self.get_delete_view_class()

    class MyDeleteView(delete_view_class): # add self.mixin if you use it.
        def get_success_url(self):
            pass # see below

Now, with that much shorter snippet. Your issue is missing some information, like what do you mean by "did not work", do you have any errors?

On your example you access self.project.id but from the posted code I do not see when are you setting this parameter.

Regardless, what you want to do is override the get_success_url() (ie, the 'pass' in the snippet above) and return the url to redirect to. Here you are basically on the standard django DeleteView, so you should have access to self.object which contains the object about to be deleted.

Note that get_sucess_url() is called before calling self.object.delete(). So, guessing from your snippet, you want:

def get_delete_view(self):
    delete_view_class = self.get_delete_view_class()

    class MyDeleteView(delete_view_class): # add self.mixin if you use it.
        def get_success_url(self):
            return "/Project/" + self.object.project.id + "/interfacelist")

On a side note, you should use reverse() instead of hardcoding the url like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants