Skip to content Skip to sidebar Skip to footer

How To Submit And Retrieve Data In One View In Django?

I have a little confusion in my app. I have an HTML page in which I have added one form (form data submitting) and one table (to display the submitted data). For which I have a vie

Solution 1:

You can revise your view function to this:

def add_staff_type(request):
    if request.method == 'POST':
        designation = request.POST.get('designation')
        salary = request.POST.get('salary')
        datetime = request.POST.get('datetime')

        add_staff_type = staff_type.objects.create(designation=designation, salary=salary, datetime=datetime)
        add_staff_type.save()  
        messages.info(request, "Staff type has been Added.")
        # this can also be return redirect('staff:add_staff_type')
        return redirect('add_staff_type')
    
    #fetching records from the database table
    display_staff_type = staff_type.objects.all()
    return render(request, 'staff/staff_type.html',{'display_staff_type':display_staff_type})

Since you are calling the same view and serving data to the same form and view you can remove the action value of the form which goes like this:

<form
    class="needs-validation"
    action=""
    method="POST"
    enctype="multipart/form-data"
    novalidate
  >

Post a Comment for "How To Submit And Retrieve Data In One View In Django?"