[Django]-Django didn't return an HttpResponse object

7👍

You are getting this problem because you havn’t written a HttpResponse object if request type is not POST

To overcome this in your view write somthing which will process if request type is not post

def search_page(request):
    form = SearchForm()
    if request.method == "POST":
        f = SearchForm(request.POST)
        if f.is_valid():
            Pets = Pet.objects.filter(animal = f.cleaned_data["text"])
            return HttpResponseRedirect("search.html",{"Pets":Pets},{"form":form})



    return render_to_response("search.html",{"form":form} , context_instance = RequestContext(request))

Hope this will help you thanks

1👍

The error is because when the function is called the method type is not POST and it does not find the corresponding HttpResponse object.

def search_page(request):
    form = SearchForm()
    if request.method == "POST":
        f = SearchForm(request.POST)
        if f.is_valid():
            Pets = Pet.objects.filter(animal = f.cleaned_data["text"])
            return HttpResponseRedirect("search.html",{"Pets":Pets},{"form":form})
        else:
            return render_to_response("search.html",{"form":form} , context_instance = RequestContext(request))

    return render_to_response("any.html",{} , context_instance = RequestContext(request))
👤Nilesh

0👍

def addsponser(request):
if request.method == ‘POST’:
# return HttpResponse(request,’error is here’)

    if (request.POST.get('firstname') and
            request.POST.get('lastname') and
            request.POST.get(' email') and
            request.POST.get('phone_Number') and
            request.POST.get('gender') and
            request.POST.get('state') and
            request.POST.get('adress') and
            request.POST.get('postal_code') and
            request.POST.get('town')
            ):
            fname = request.POST.get('firstname')
            lname =  request.POST.get('lastname')
            em = request.POST.get(' email')
            phn = request.POST.get('phone_Number')
            gnd = request.POST.get('gender')
            stt = request.POST.get('state')
            add =  request.POST.get('adress')
            pstc = request.POST.get('postal_code')
            twn = request.POST.get('town')           

            try:
                sponser = Sponsers()
                sponser.firstname = fname
                sponser.lastname = lname
                sponser.email = em
                sponser.Phone_Number = phn
                sponser.gender = gnd
                sponser.state = stt
                sponser.adress = add
                sponser.postal_code = pstc
                sponser.town = twn
                sponser.save()               
                
                messages.success(request, "sponser Added")
                return redirect('sponsers')
            except Exception:
                messages.error(request, "Failed to add sponser")
                return redirect('sponsers')
    else:           
        pass
else:
    return redirect('sponsers')

Leave a comment