killerbees
killerbees

Reputation: 209

Edit form in django doesn't save record after update

I have a little problem I've create a edit form to update the existing records. The form is displaying correctly, but when I click the edit button to update the records, the url redirecting me and record is not updated. My views.py resposible for edit:

@login_required
def szczegoly_pracownik(request, id):
    link_pracownik = get_object_or_404(Cudzoziemiec, id=id)
    return render(request, 'cudzoziemiec/szczegoly_pracownik.html', {'link_pracownik': link_pracownik})

@login_required
def edycja_pracownika(request, id):
    link_pracownik = get_object_or_404(Cudzoziemiec, id=id)
    if request.method == 'POST':
        edycja_pracownika = CudzoziemiecForm(request.POST, instance=link_pracownik)
        if edycja_pracownika.is_valid():
            link_pracownik = edycja_pracownika.save(commit=False)
            link_pracownik.save()
            return render('szczegoly_pracownik', id=link_pracownik.id)
    else:
        edycja_pracownika = CudzoziemiecForm(request.user, instance=link_pracownik)
    return render(request, 'cudzoziemiec/edycja_pracownika.html', {'edycja_pracownika': edycja_pracownika})

The def szczegoly_pracownika is responsible for displaying the detail view

File edycja_pracownika.html

{% if request.user.is_authenticated %}
<form action="." method="post">
    {{ edycja_pracownika.as_p }}
    {% csrf_token %}
    <div class="float-right">
    <p><input type="submit" value="Edytuj" ></p>
{% endif %}

and the urls.py responsible for detail view and edit view

...
    path('pracownik/<id>', edycja_pracownika, name='edycja_pracownika'),
    path('pracownik/<id>/', szczegoly_pracownik, name='szczegoly_pracownik'),

Maybe somebody know where is the bug? EDIT:

forms.py

class CudzoziemiecForm(forms.ModelForm):
    class Meta:
        model = Cudzoziemiec
        fields = ('nazwa','imie', 'nazwisko','obywatelstwo', 'data_ur','miejsce_ur','paszport','biometria', 'data_start_pasz', 'data_koniec_pasz', 'dok_pobytowy','cel_wizy', 'data_start_pobyt', 'data_koniec_pobyt')

    def __init__(self, user, *args, **kwargs):            
        super(CudzoziemiecForm, self).__init__(*args, **kwargs)
        self.fields['nazwa'].queryset = user.firma_set.all() 
        self.user = user

    def save(self, commit=True):
        instance = super(CudzoziemiecForm, self).save(commit=False)
        instance.user = self.user
        if commit:
            instance.save()
        return instance

Upvotes: 0

Views: 260

Answers (1)

barraquito
barraquito

Reputation: 178

I don't arrive to see the real problem but a couple of things that I would try.

You are using the same almost the same url for different pages. It should not be a problem, but I see that as a possible pitfall. Why don't use something like r'^pracownik/edytuj/$' for the editing form? (sorry for my attempt to make a Polish url :-) ).

Maybe that can avoid possible problems and help to clarify the error.

Also when you say that:

the url redirecting me

Do you mean you are redirected to the form again or to the detail page?

Upvotes: 1

Related Questions