Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

82
Visualizações
Submitting a form inside a detail view creates a new object instead of replacing it

I’m trying to create a dashboard for the staff users to fill in and edit some information regarding their users. The form works and saves successfully, but when I submit it for a second time, it creates a new object. It won’t replace the previous:

This is my views.py file:

class ScientificInfoView(FormMixin, DetailView):
    model = ScientificInfo
    template_name = 'reg/scientific-info.html'
    form_class = ScientificInfoForm

    def get_success_url(self):
        return reverse('scientific-info', kwargs={'pk': self.object.pk})

    def get_context_date(self, **kwargs):
        context = super(ScientificInfoView, self).get_context_data(**kwargs)
        context['form'] = ScientificInfoForm()
        return context

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        form = self.get_form()
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

    def form_valid(self, form):
        form.save()
        return super(ScientificInfoView, self).form_valid(form)

And my template:

<form method="POST" enctype="multipart/form-data" action="{% url 'scientific-info' pk=object.id %}">
    {% csrf_token %}
    {{form}}
    <button type="submit">submit</button>
</form>

File urls.py:

path('surveys/scientific/<pk>', login_required(views.ScientificInfoView.as_view()), name='scientific-info')

I’m pretty sure that the action part in my form is causing the issue, but how can I solve it?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use this:

def get_success_url(self):
    pk = self.kwargs["pk"]
    return reverse("scientific-info", kwargs={"pk": pk})

Or

class ScientificInfoView(FormMixin, DetailView):
    model = ScientificInfo
    template_name = 'reg/scientific-info.html'
    form_class = ScientificInfoForm
    def get_success_url(self):
         return reverse("scientific-info", args=[pk]) # You can replace pk
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda