Django没有返回一个HttpResponse对象 [英] Django didn't return an HttpResponse object

查看:854
本文介绍了Django没有返回一个HttpResponse对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个简单的宠物店应用程序,只是添加了搜索框功能,我收到这个错误

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.

我试图将render_to_response更改为HttpResponseRedirect,但仍然有相同的错误。

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

链接回视图中的search_page函数。

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

我做了一些研究,我理解一个视图必须返回一个HttpResponse,当一个HttpRequest被创建和render_to_response只是一个快捷方式。有人帮助解释为什么这个功能将无法工作。谢谢你/ / p >

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

推荐答案

你正在得到的问题,因为如果请求类型不是 POST

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

为了克服这个在你的看法写somthing,将处理,如果请求类型不发布

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

希望这将有助于您感谢

这篇关于Django没有返回一个HttpResponse对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆