i am making a django website with multiple forms also used foregin key ValueEror :The view Capp.views.InsertProduct didn't return anHttpResponseobject

i am making a django website with multiple forms also used foregin key(user_id) to link one form with other in the database but at the last i get value error the error is:Exception Type: ValueError Exception Value:
The view Capp.views.InsertProduct didn't return an HttpResponse object. It returned None insteated , the following is view.py file code(not complete code but only where error can lie)models.py part

def InsertProduct(request):
    if request.method == 'POST':
        if request.POST.get('user_id') and request.POST.get('pname') and request.POST.get('pcategory') and request.POST.get('pdetails') and request.POST.get('foundedin') and request.POST.get('orderoftest') and request.POST.get('t1') and request.POST.get('t2') and request.POST.get('t3') and request.POST.get('f1') and request.POST.get('f2') and request.POST.get('f3') and request.POST.get('f4') and request.POST.get('f5'):
            saveproduct = ProInsert()
            saveproduct.user_id = request.POST.get('user_id')
            saveproduct.pname = request.POST.get('pname')
            saveproduct.pcategory = request.POST.get('pcategory')
            saveproduct.pdetails = request.POST.get('pdetails')
            saveproduct.foundedin = request.POST.get('foundedin')
            saveproduct.orderoftest = request.POST.get('orderoftest')
            saveproduct.t1 = request.POST.get('t1')
            saveproduct.t2 = request.POST.get('t2')
            saveproduct.t3 = request.POST.get('t3')
            saveproduct.f1 = request.POST.get('f1')
            saveproduct.f2 = request.POST.get('f2')
            saveproduct.f3 = request.POST.get('f3')
            saveproduct.f4 = request.POST.get('f4')
            saveproduct.f5 = request.POST.get('f5')

            checkpname = ProInsert.objects.filter(
                pname=saveproduct.pname).first()
             return render(request, 'product_details.html')#here I had add what u said sir

            if checkpname:
                msgpname = messages.success(request, 'The user with Product Name ' +
                                            request.POST['pname']+' already exist...!')

                return render(request, 'product_details.html', {'msgpname': msgpname})
            saveproduct.save()
            messages.success(request, 'Product Added..!')

            return render(request, 'product_details.html')

    else:
        return render(request, 'product_details.html')

1 answer

  • answered 2021-07-28 04:23 Ashish Nautiyal

    I think you got it wrong I have update your code and commented the part i was telling you.

    def InsertProduct(request):
        if request.method == 'POST':
            if request.POST.get('user_id') and request.POST.get('pname') and request.POST.get('pcategory') and request.POST.get('pdetails') and request.POST.get('foundedin') and request.POST.get('orderoftest') and request.POST.get('t1') and request.POST.get('t2') and request.POST.get('t3') and request.POST.get('f1') and request.POST.get('f2') and request.POST.get('f3') and request.POST.get('f4') and request.POST.get('f5'):
                saveproduct = ProInsert()
                saveproduct.user_id = request.POST.get('user_id')
                saveproduct.pname = request.POST.get('pname')
                saveproduct.pcategory = request.POST.get('pcategory')
                saveproduct.pdetails = request.POST.get('pdetails')
                saveproduct.foundedin = request.POST.get('foundedin')
                saveproduct.orderoftest = request.POST.get('orderoftest')
                saveproduct.t1 = request.POST.get('t1')
                saveproduct.t2 = request.POST.get('t2')
                saveproduct.t3 = request.POST.get('t3')
                saveproduct.f1 = request.POST.get('f1')
                saveproduct.f2 = request.POST.get('f2')
                saveproduct.f3 = request.POST.get('f3')
                saveproduct.f4 = request.POST.get('f4')
                saveproduct.f5 = request.POST.get('f5')
    
                checkpname = ProInsert.objects.filter(
                    pname=saveproduct.pname).first()
                # return render(request, 'product_details.html')# NO need to add here as code below will be dead in this case.
                if checkpname:
                    msgpname = messages.success(request, 'The user with Product Name ' +
                                                request.POST['pname']+' already exist...!')
                    return render(request, 'product_details.html', {'msgpname': msgpname})
                saveproduct.save()
                messages.success(request, 'Product Added..!')
                return render(request, 'product_details.html')
            else:
                return render(request, 'product_details.html')# What i meant was to add it here 
        else:
            return render(request, 'product_details.html')
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum