Edit form inputs

Let’s say I have a form that allows the user to input data as many times as they want. Each form is saved and individually shown in its corresponding boxes, each of which has an edit button that loads up a modal. How can someone change/ update the input data in that specific box after clicking edit without chnaging/updatingh the input data for the other boxes.

 if request.method == "POST":
        form = application_form(request.POST)
        if form.is_valid():
            application = form.save(commit=False)
            application.user = request.user
            application.save()
            return redirect('track_applications')
    else:
        form = application_form()

It’s not clear to me what the situation is that you’re describing. I’m not sure I understand the nature of the ui you’re working with, or what you mean by “corresponding boxes”.

I think it would be helpful here if provided more details about what you’re trying to do, including the models, templates, forms and views involved with this.

I’m building a job application tracking system where users can add multiple job applications through a form. Each saved application displays as a card/box on the page, and each box has an “Edit” button that should ope a modal with the form pre-populated with that specific application’s data which then be changed/updated.

My Problem: When a user clicks “Edit” on a specific application box, I want to update only that particular application record, not create a new one or accidentally update a different application.

Current Setup:

class applications(models.Model):
    STATUS_CHOICE = [
        ('applied', 'Applied'),
        ('rejected', 'Rejected'),
        ("assessment", "Assessment"),
        ("interview", "Interview"),
        ("offer", "Offer")
    ]
    
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    company = models.CharField(max_length=40)
    position_title = models.CharField(max_length=40)
    apply_date = models.DateField()
    status = models.CharField(max_length=40, choices=STATUS_CHOICE)
    location = models.CharField(max_length=40)
    salary = models.FloatField(max_length=11, null=True, blank=True)
@login_required
def track_applications(request):
    if request.method == "POST":
        form = application_form(request.POST)
        if form.is_valid():
            application = form.save(commit=False)
            application.user = request.user
            application.save()
            return redirect('track_applications')
    else:
        form = application_form()
    
    user_applications = applications.objects.filter(user=request.user)
    context = {'form': form, 'user_application': user_applications}
    return render(request, "track_applications.html", context)
<!-- Each application displays as a box -->
{% for application in user_application %}
<div class="job-box">
    <div class="job-header">
        <span class="job_company">{{application.company}}</span>
        <span class="job_position_title">{{application.position_title}}</span>
        <!-- other application details -->
    </div>
    <div class="edit-btt">
        <button class="edit-btn">Edit</button>  <!-- This opens the modal -->
    </div>
</div>
{% endfor %}

<!-- Single modal that reuses the same form -->
<div class="modal" id="modal">
    <form method="post">
        {% csrf_token %}
        {{form.company}}
        {{form.position_title}}
        <!-- rest of form fields -->
        <button type="submit">Save</button>
    </form>
</div>

What I Need Help With:

  1. How do I modify my view to handle both creating new applications AND updating existing ones?
  2. How can I pass the specific application ID to the form when the edit button is clicked, so Django knows which record to update?
  3. What’s the best way to pre-populate the modal form with the existing application data when editing?

The idea I think would work is to get that specific application id by querying through the db and then use instance=request.user (or a similar and more specific version for the problem) to populate the edit form with.

This isn’t really a Django issue. Django itself isn’t going to open a “modal” for you - that’s work that needs to be done in the browser using JavaScript.

This means your JavaScript will be responsible for identifying which edit button was clicked, and copying the appropriate data to the modal form.

Also I think that this use case looks a lot like a “formset-ish” situation, I don’t have much experience using them. But I think that would apply here.