I am trying to create a form where parent and child relationships can be handled in one form through inlineformset, when i try to update the applicant, or add another one, i keep getting noReverseMatch error. Kindly help am new to inlineformset

NoReverseMatch at /applicant-dependant-update/2/
Reverse for 'applicants' with keyword arguments '{'pk': 2}' not found. 1 pattern(s) tried: ['\\Z']

Error message

NoReverseMatch at /applicant-dependant-update/2/
Reverse for 'applicants' with keyword arguments '{'pk': 2}' not found. 1 pattern(s) tried: ['\\Z']

Error message

Models.py

class Applicant(models.Model):
    application_number = models.IntegerField(null=False, blank=False, unique=True)
    region = models.CharField(max_length=20, choices=REGION_CHOICES)
    family_name = models.CharField(null=False, blank=False, max_length=255)
    first_name = models.CharField(null=False, blank=False, max_length=255)
    middle_name = models.CharField(blank=True, max_length=255)
    gender = models.CharField(max_length=10, null=False, choices=GENDER_CHOICES)
    month_B = models.CharField(null=False, choices=MONTH_CHOICES, max_length=100)
    day_B = models.PositiveIntegerField(blank=False, null=False)
    year_B = models.PositiveIntegerField(blank=False, null=False)
    birth_city = models.CharField(max_length=255, null=False)
    birth_country = models.CharField(max_length=255, null=False)


class Dependant(models.Model):
    main_applicant = models.ForeignKey('Applicant', blank=False, null=False,  on_delete=models.CASCADE,related_name='applicant_dependant')
    dress_color = models.CharField(blank=True, max_length=50)
    family_name = models.CharField(null=False, blank=False, max_length=255)
    first_name = models.CharField(null=False, blank=False, max_length=255)
    middle_name = models.CharField(blank=True, max_length=255)
    gender = models.CharField(max_length=100, choices=GENDER_CHOICES)
    month_B = models.CharField(blank=False, choices=MONTH_CHOICES, max_length=100)
    day_B = models.PositiveIntegerField(blank=False)
    year_B = models.PositiveIntegerField(blank=False)
    birth_city = models.CharField(max_length=255)
    birth_country = models.CharField(max_length=255)

Urls.py

urlpatterns = [
    path('login/', UserLogin.as_view(), name='login'),
    path('logout/', LogoutView.as_view(next_page='login'), name='logout'),
    path('register/', RegisterPage.as_view(), name='register'),
    path('', TaskList.as_view(), name='tasks' ),
    path('task/<int:pk>/', TaskDetail.as_view(), name='task' ),
    path('create-task/', TaskCreate.as_view(), name='create-task'),
    path('update-task/<int:pk>/', TaskUpdate.as_view(), name='update-task'),
    path('delete-task/<int:pk>/', TaskDelete.as_view(), name='delete-task'),
]

Views.py

class ApplicantDependantUpdate(SingleObjectMixin, FormView):

    model = Applicant
    template_name = 'dataentry/applicant_dependant_update.html'

    def get(self, request, *args, **kwargs):
        self.object = self.get_object(queryset=Applicant.objects.all())
        return super().get(request, *args, **kwargs)
    
    def post(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            return HttpResponseForbidden()
        self.object = self.get_object(queryset=Applicant.objects.all())
        return super().post(request, *args, **kwargs)
    
    def get_form(self, form_class = None):
        return ApplicantDependantFormset(**self.get_form_kwargs(), instance=self.object)
    
    def form_valid(self, form):
        form.save(commit=False)

        messages.add_message(
            self.request,
            messages.SUCCESS,
            'Changes were saved'
        )

        return HttpResponseRedirect(self.get_success_url())
    
    def get_success_url(self):
        return reverse('applicants', kwargs={'pk': self.object.pk})

template

{% block content %}

<form action="" method="post" enctype="multipart/form-data">
  {% for hidden_field in form.hidden_fields %}
    {{ hidden_field.errors }}
    {{ hidden_field }}
  {% endfor %}

  {% csrf_token %}

  {{ form.management_form }}
  {{ form.non_form_errors }}

  <h3>Update Applicant Dependant Information</h3>
  {% for Dependant_form in form.forms %}
    <hr>
    <h5>
      {% if Dependant_form.instance.id %}
        Dependant: {{ Dependant_form.instance.family_name }}
      {% else %}

        {% if form.forms|length > 1 %}
          Add another Dependant
        {% else %}
          Add Dependant
        {% endif %}

      {% endif %}
    </h5>

    {% for hidden_field in Dependant_form.hidden_fields %}
      {{ hidden_field.errors }}
    {% endfor %}
    <!-- {{ Dependant_form.dress_color|as_crispy_field }} CAN STAND ALONE AND BE FORMATED IN DIV-->
    <table>
      {{ Dependant_form.as_table }}
    </table>
  {% endfor %}
  <hr>
    <p>
      <button type="submit" value="Update Applicant" class="text-white bg-gradient-to-br from-green-400 to-blue-600 hover:bg-gradient-to-bl focus:ring-4 focus:outline-none focus:ring-green-200 dark:focus:ring-green-800 font-medium rounded-lg text-sm px-5 py-2.5 text-center mr-2 mb-2">Submit Dependant
      </button>
    <a href="{% url 'applicants' %}">Cancel</a>
    </p>


</form>
0

There are 0 best solutions below