Home > Back-end >  Django generic.UpdateView NOT updating values upon successful POST
Django generic.UpdateView NOT updating values upon successful POST

Time:07-16

Below I am trying to update a specific form (OrganizationForm):

  1. Template is Rendering with the form fields and existing values in the fields.
  2. Upon Submission of the form the POST request is successful.

ISSUE: The changes are not being saved to the DB.

# Views.py

class OrganizationDetails(LoginRequiredMixin, UpdateView):
    login_url = '/user/login/'
    redirect_field_name = 'redirect_to'
    model = Organization
    form_class = OrganizationForm
    template_name = 'organization/org_details.html'
    success_url = reverse_lazy("organization")

    def get_object(self):
        queryset = super().get_queryset()
        return queryset.get(pk=self.request.user.organization.pk)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        curr_org = self.request.user.organization
        heading = TermsHeading.objects.filter(organization=curr_org)
        content = TermsContent.objects.filter(heading__organization=curr_org)
        context['headings'] = heading

        data = dict()
        for item in heading:
            x = []
            for desc in content:
                if desc.heading == item:
                    x.append(desc)
            data[item] = x
        context['data'] = data
        return context

What I have tried:

I have overrided the 'POST' method in the class with the following:


    # def post(self, request):
    #     print(request.POST)
    #     form = OrganizationForm(request.POST)
    #     if form.is_valid():
    #         form.save()
    #         print("Valid and Saved")
    #     else:
    #         print(form.errors)
    #     return super(OrganizationDetails, self).post(request)

Terminal Output:

[16/Jul/2022 02:44:15] "GET /user/myorganization/ HTTP/1.1" 200 29699
Not Found: /favicon.ico
[16/Jul/2022 02:44:15] "GET /favicon.ico HTTP/1.1" 404 5037
[16/Jul/2022 02:44:25] "POST /user/myorganization/ HTTP/1.1" 302 0
[16/Jul/2022 02:44:25] "GET /user/myorganization/ HTTP/1.1" 200 29699
Not Found: /user/myorganization/Roboto-Regular.ttf
[16/Jul/2022 02:44:25] "GET /user/myorganization/Roboto-Regular.ttf HTTP/1.1" 404 6841
[16/Jul/2022 02:44:25] "GET /user/myorganization/ HTTP/1.1" 200 29699

Please Help me with the above, Thanks.

CodePudding user response:

Check form_valid() in official documentation.

Also, the get_object() seems to have bugs. You may be able to use it as follows.

def get_object(self):
    return self.request.user.organization

CodePudding user response:

I think there are few things going wrong with your code and do check the below :

  1. If you are updating existing data in the DB, then you should override 'PUT' method instead of 'POST'. Something like this def put (self, request):

  2. Check the form field in the OrganizationForm class that allows the request to save in the DB.

  3. Check form_valid().

  • Related