【发布时间】:2010-12-25 08:29:09
【问题描述】:
Django 新手问题....
我正在尝试编写一个搜索表单,并在搜索请求和搜索结果之间保持输入框的状态。
这是我的表格:
class SearchForm(forms.Form):
q = forms.CharField(label='Search: ', max_length=50)
这是我的观点代码:
def search(request, q=""):
if (q != ""):
q = q.strip()
form = SearchForm(initial=q)
#get results here...
return render_to_response('things/search_results.html',
{'things': things, 'form': form, 'query': q})
elif (request.method == 'POST'): # If the form has been submitted
form = SearchForm(request.POST)
if form.is_valid():
q = form.cleaned_data['q']
# Process the data in form.cleaned_data
return HttpResponseRedirect('/things/search/%s/' % q) # Redirect after POST
else:
form = SearchForm()
return render_to_response('things/search.html', {
'form': form,
})
else:
form = SearchForm()
return render_to_response('things/search.html', {
'form': form,
})
但这给了我错误:
Caught an exception while rendering: 'unicode' object has no attribute 'get'
如何传递初始值?我尝试过的各种事情似乎都会干扰 request.POST 参数。
【问题讨论】:
标签: django django-forms