Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

71
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!