UpdateView and def post()

30 views
Skip to first unread message

MH

unread,
Jun 2, 2020, 1:05:45 PM6/2/20
to Django users
Hi

I want to modidy my view that is based on UpdateView and it should work when called via post method.

This is what I have so far and I am not sure if it makes sense or if it could be done in an easier way.

class GD_Update(UpdateView):
# setting the model, form_class and template_name
    model
= ImmoGrunddaten
    form_class
= form_immogrunddaten_anlegen
    template_name
= templ_folder_name + 'gd_update.html'

# what should happen if the updateview was successful
   
def get_success_url(self):
        gdid
= self.object.pk
       
return reverse_lazy(url_app_name + 'gd_ansehen', kwargs={'pk':gdid})

# I guess that I need this in order to get the data from the database and to populate the form
   
def get_object(self, queryset=None):
       
self.queryset = ImmoGrunddaten.objects.get(id = self.to_update)
       
return self.queryset

# Here is the handling of the post calls
   
def post(self, request, pk):
# this is when I did not push a button like "confirm update", meaning the first call of the view via post method
       
if not self.request.POST.get('action', False) == 'aktualisieren':
           
self.to_update = pk
           
self.object = self.get_object()
           
return super(GD_Update, self).post(request, pk)
       
else:
# and this is what happens if the confirm button was pushed. Now I want to save the changes to the database. But I don't know how.
            context
= {
                   
'fehlermeldung': 'das update handling wurde noch nicht hinterlegt'
                   
}
           
return render(request, templ_folder_name + 'fehlerseite.html', context)


Reply all
Reply to author
Forward
0 new messages