3

I made a simple pet store app and just added search box feature and I received this error

ValueError at /pet/search/
The view mysite.pet.views.search_page didn't return an HttpResponse object.

I tried to change render_to_response into HttpResponseRedirect but still got the same error.

Linking back to my search_page function in views.

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))

I did some research and I understand a view has to return a HttpResponse when a HttpRequest is made and render_to_response is just a shortcut.Can someone help explain why this function won't work.Thank you

donkeyboy72
  • 1,883
  • 10
  • 37
  • 55

3 Answers3

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

masterofdestiny
  • 2,751
  • 11
  • 28
  • 40
  • You've got two identical calls to render_to_response - the first is redundant. You could drop the else statement and leave the one call right at the end. – Aidan Ewen Mar 05 '13 at 06:47
  • @masterofdestiny sorry . It said wait 4 min to accept the answer then I forgot about it – donkeyboy72 Mar 05 '13 at 07:03
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))
donkeyboy72
  • 1,883
  • 10
  • 37
  • 55
Nilesh
  • 167
  • 1
  • 4
  • 17
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')