[Fixed]-Django: submit POST to create objects not working

1👍

You can follow these steps to figure out the reason:

  1. Make sure, when you click the Submit button, the page is reloaded;
  2. Check the page source code to make sure the content of the form is correct;
  3. Before you click the submit button, make sure every field in the logout page is valued and the value is correct;
  4. Change your code and add “else” section as below:

    def update_sample_logout (request, project_id):

    if request.method == 'GET':
        ......
    
    project = Project.objects.get(id = project_id)
    
    log_form = LogOutForm(request.POST or None, request.FILES or None)
    if request.method == 'POST':
        if log_form.is_valid():
            ......
        ***else:
            # go to another page or print out some information you can see.***
    
  5. If everything above is working, you can comment out the this section as below:

    if log_form.is_valid():
        # in_or_out = log_form.cleaned_data['in_or_out']
        # logout_date = log_form.cleaned_data['logout_date']
        # logout_by = log_form.cleaned_data['logout_by']
        # out_shipping_method_tracking_number =  log_form.cleaned_data['out_shipping_method_tracking_number']
        # out_notes = log_form.cleaned_data['out_notes']
    

AND released one by one to test which is not correct.

Good Luck!

0👍

Looks like that there is nothing wrong with your code.

I believe that you loose loglist GET parameter at some point, so the view just redirect you to “projstatus:project_detail” page.

👤vasi1y

0👍

Please check this section:

if 'loglist' in request.GET:
    sample_detail_list = request.GET.getlist('loglist')
    samples = SampleDetail.objects.filter(id__in = sample_detail_list)
else:
    messages.error(request, "Did not select sample" )
    return HttpResponseRedirect(reverse('projstatus:project_detail', args=(project_id,)))

So, when you finished the form and click the “SAVE” button, the method for request is “POST”, then the page will go to the projstatus:project_detail.

if you want to check the method in the same method, you can use like this:

def update_sample_logout (request, project_id):

    if request.method == 'GET':
        if 'loglist' in request.GET:
            sample_detail_list = request.GET.getlist('loglist')
            samples = SampleDetail.objects.filter(id__in = sample_detail_list)
        else:
            messages.error(request, "Did not select sample" )
            return HttpResponseRedirect(reverse('projstatus:project_detail', args=(project_id,)))


    project = Project.objects.get(id = project_id)

    log_form = LogOutForm(request.POST or None, request.FILES or None)


    if request.method == 'POST':
        if log_form.is_valid():
            ......

Leave a comment