Search code examples
djangodjango-viewsdjango-generic-views

Django Generic Based view: not saving HTML form data to database


I have a Django library application with several books and authors, here is a /author/create html form used by the admin to create/update details of the author (firstname, lastname, dob, profile picture), referring the MDN Library application.

I have a Generic Class Based View for this purpose:

 class AuthorCreate(PermissionRequiredMixin, CreateView):
    permission_required = 'is_superuser'
    model = Author
    fields = '__all__'
    success_url = reverse_lazy('author-detail')


  class AuthorUpdate(PermissionRequiredMixin, UpdateView):
    permission_required = 'is_superuser'
    model = Author
    fields = ['first_name', 'last_name', 'date_of_birth', 'date_of_death']
    success_url = reverse_lazy('author-detail')

 class AuthorDelete(PermissionRequiredMixin, DeleteView):
    permission_required = 'is_superuser'
    model = Author
    success_url = reverse_lazy('authors')

And these are the url patterns:

    urlpatterns += [
path('author/create/', views.AuthorCreate.as_view(), name='author_create'),  # redirects to author_form.html
path('author/<int:pk>/update/', views.AuthorUpdate.as_view(), name='author_update'),  # redirects to author_form.html
path('author/<int:pk>/delete/', views.AuthorDelete.as_view(), name='author_delete'),  # redirects to author_confirm_delete.html
     ]

And this is the author_form.html for creating/updating author details:

     <form action="" method="post" class="form-horizontal" enctype="multipart/form-data">
            {% csrf_token %}
      //remaining code...
     </form>

Now, on clicking the submit button in the html form above, it should redirect to author/id page(mentioned in the success_url), however the main concern is that a new author is not getting created in the first place. I am not sure how the html form data is being saved, whether or not it is being saved in the first place, because the page is redirecting to the success_url.

Code Referred from MDN: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Forms


Solution

  • In your Author model add,

    def get_absolute_url(self):
          return reverse('author_detail', kwargs={'pk': self.pk}) #add kwargs and app_name depending upon your requirement.
    

    views.py

    class AuthorCreate(PermissionRequiredMixin, CreateView):
        permission_required = 'is_superuser'
        model = Author
        fields = '__all__'
    
    class AuthorUpdate(PermissionRequiredMixin, UpdateView):
       permission_required = 'is_superuser'
       model = Author
       fields = ['first_name', 'last_name', 'date_of_birth', 'date_of_death']
    

    Other way if you are using form in CreateView and UpdateView,

    def form_valid(self, form):
         self.object = form.save()
         # ....
         return HttpResponseRedirect(self.get_success_url())
    

    If you want to go to previous page only, then this SO question has an answer