简单的搜索表单,ValueError at / search /视图AddressApp.views.search没有返回HttpResponse对象

时间:2015-11-11 11:46:53

标签: python django

我想在Django中创建一个简单的搜索表单,通过文本框获取person的地址并显示Firstname,lastname和state。 但是我遇到这个错误很麻烦:

ValueError at /search/
The view AddressApp.views.search didn't return an HttpResponse object.
Request Method: GET
Request URL:    http://127.0.0.1:8000/search/
Django Version: 1.6.11

views.py

from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render
from AddressExtractor import *

def search(request):
    if __name__ == '__main__':
        test = AddressExtractor(str(request.GET['q']))
        firstname = test.getFN() # get firstname
        lastname = test.getLN() # get lastname
        state = test.getState()  #get state
        t = render((request, 'main.html', {"firstname": firstname, "lastname": lastname, "state": state}))
        return HttpResponse(t)

urls.py

url(r'^search/', 'AddressApp.views.search', name='search'),

template:main.html

<form id="main_form" method="GET" action="/search/" >
    <input type="text" id="q" name="q"/>
    <input type="submit" value="SEARCH">
</form>
<DIV>{{ firstname }}
{{ lastname }}
{{ state }}</DIV>

感谢所有

1 个答案:

答案 0 :(得分:2)

为什么要将xcworkspace放入您的功能中?不要这样做,它不属于那里,在视图中永远不会成真。