Problem with success_url in Django

Asked

Viewed 182 times

2

In views I have the following classes

class AdicionarPessoaView(CustomCreateView):
    template_name = "cadastro/adicionar.html"
    form_class = PessoaForm
    model = Pessoa
    obj = Pessoa.objects.all().count()
    if obj > 0:
        obj = Pessoa.objects.last()
        success_url = reverse_lazy('cadastro:add_pessoa_view', kwargs={'pk': obj.id})
    success_message = "Pessoa cadastrada com sucesso!"

    def get_context_data(self, **kwargs):
        context = super(AdicionarPessoaView,
                        self).get_context_data(**kwargs)


class EditarPessoaView(CustomUpdateView):
    template_name = "cadastro/editar.html"
    form_class = PessoaForm
    model = Pessoa
    obj = Pessoa.objects.all().count()
    if obj > 0:
        obj = Pessoa.objects.last()
        success_url = reverse_lazy('cadastro:editar_pessoa_view', kwargs={'pk': obj.id})

    success_message = "Pessoa editada com sucesso!"

    def get_context_data(self, **kwargs):
        context = super(EditarPessoaView,
                        self).get_context_data(**kwargs)
        return context

My intention is that when it comes submit on the person’s registration page the new page is that person’s editing page. But the way I’m using, after adding or editing a person, the next page is opening with the id from the previous register. For example, if I now register a pessoa with the id 12 and give submit the page you should open would be meusite.com/cadastro/editar/12 , but it’s opening up meusite.com/cadastro/editar/11.

I’ve tried this before: Instead of using the variable sucess_url, I used this function in both classes:

def get_success_url(self):
    return reverse('cadastro:editar_pessoa_view', kwargs={'pk': self.kwargs('pk')})

However, in giving submit on the page I get this error:

TypeError at /cadastro/editar/12/
argument of type 'NoneType' is not iterable

During handling of the above exception (Reverse for 'None' not found. 'None' is not a valid view function or pattern name.)

What is the correct way for the next page to be the editing page of the object I am adding/editing?

EDIT

Doing some tests I realized that the function get_success_url(self) is not "working", because when I use it the way below, with the variable success_url, when editing a person and giving submit I am redirected to a new registration page.

class EditarPessoaView(CustomUpdateView):
    template_name = "cadastro/editar.html"
    form_class = PessoaForm
    model = Pessoa
    success_url = reverse_lazy('cadastro:adicionar_pessoa_view')

    def get_context_data(self, **kwargs):
        context = super(EditarPessoaView,
                        self).get_context_data(**kwargs)
        return context

But when using the function:

def get_success_url(self):
    return reverse('cadastro:adicionar_pessoa_view')

get the same error:

TypeError at /cadastro/editar/12/
argument of type 'NoneType' is not iterable

During handling of the above exception (Reverse for 'None' not found. 'None' is not a valid view function or pattern name.)

What’s wrong for that function get_success_url is not working?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.