Add a [save and copy] button

I have this form:

<div class="form">
        <h1>Shift form</h1>
        <form method="post" action="{% url 'shift-form' %}">
            {% csrf_token %}

            <label for="id_status">Status:</label>
            <select id="id_status" name="status" class="form-select">
                {% for choice in form.status.field.choices %}
                    <option value="{{ choice.0 }}">{{ choice.1 }}</option>
                {% endfor %}
            </select>


            <label for="id_shift_title">Shift Title:</label>
            <input type="text" id="id_shift_title" name="shift_title" class="form-control">

            <label for="id_function">Function:</label>
            <select id="id_function" name="function" class="form-select">
                {% for choice in form.function.field.choices %}
                    <option value="{{ choice.0 }}">{{ choice.1 }}</option>
                {% endfor %}
            </select>


            <label for="id_start_date">Start Date:</label>
            <input type="date" id="id_start_date" name="start_date" class="form-control" onchange="updateEndDate()">

            <label for="id_start_time">Start Time:</label>
            <input type="time" id="id_start_time" name="start_time" class="form-control">

            <label for="id_end_date" style="display: none;">End Date:</label>
            <input type="date" id="id_end_date" name="end_date" class="form-control" style="display: none;">

            <label for="id_start_time">End Time:</label>
            <input type="time" id="id_end_time" name="end_time" class="form-control">

            <label for="id_site">Site:</label>
            <select id="id_site" name="site" class="form-select">
                {% for choice in form.site.field.choices %}
                    <option value="{{ choice.0 }}">{{ choice.1 }}</option>
                {% endfor %}
            </select>

            <label for="id_user">User:</label>
            <select id="id_user" name="user" class="form-select">
                {% for choice in form.user.field.choices %}
                    <option value="{{ choice.0 }}">{{ choice.1 }}</option>
                {% endfor %}
            </select>

            <label for="id_invoiced">Invoiced:</label>
            <input type="number" id="id_invoiced" name="invoiced" class="form-control" readonly>

            <label for="id_source">Source:</label>
            <input type="number" id="id_source" name="source" class="form-control" readonly>

            <label for="id_update">Update:</label>
            <input type="number" id="id_update" name="update" class="form-control" readonly>

            <button type="submit" name="action" value="submit" class="btn btn-primary btn-sm">Submit</button>


        </form>
    </div>

this is my forms.py:

class ShiftForm(forms.ModelForm):
    class Meta:
        model = Shift
        fields = '__all__'

And this is my view:

def shift_form(request):
    if request.method == 'POST':
        form = ShiftForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('weekdays')
    else:
        form = ShiftForm()

    return render(request, 'forms/shiftform.html', {'form': form})

Can I have a extra button with “Save and copy” That will save the record but keeps the form filled to use it for the next entry with small modifications?

Yes - when you redirect to your new view, you want to pass something as the parameter to that next page to indicate what you want to use as the initial data for the form.

A second way you could approach this would be to have your save-and-copy button perform an ajax-style call to submit the data in the current form, allowing the data to be saved without a page refresh.

I don’t understand your first suggestion. Did not use ajax until now but will look at this.
Thanks for your reply.

Is there an other (better) approuch in order to easaly add multiple records?

Sure - Django provides formsets for displaying multiple instances of the same form on one page.
(I wouldn’t necessarily say “better”, but it is another option.)

When you handle a POST, the typical flow of events is that you redirect after saving the data that was submitted. That redirect is going to cause your browser to do a GET for a url.

In this case, that url could be a view to display the same form. However, that view should take a url parameter (perhaps the pk of the row just saved), and use that parameter to fetch the data - and use that data to initialize the form. (This is probably the easiest way to implement that functionality.)

Hi Ken,

I dived in the suggested formset for displaying multiple instances of the same form. I get the impression that it is also possible to pass the initial data of the first form to the next one. Going to work on that solution.

Thanks!