장고 초보자 질문에 대한 초기 값을 전달합니다.는 .... 장고 양식 필드
여기 내 양식이다 :
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'
를? 시도한 다양한 일들이 요청을 방해하는 것 같습니다 .POST 매개 변수.
오류 인해 딕셔너리 대신 유니 코드 객체를 전달하는 것입니다. (dict는 'get()'을 정의하지만 string/unicode는 그렇지 않습니다). – spookylukey
아, 훌륭한 지적. 나는 오류를 보았지만 연결을 만들지 않았다! – cethegeek
자세한 설명과 솔루션에 감사드립니다. 결과 URL을/search/query처럼 보이기를 원했기 때문에 이런 식으로 구현했습니다. (문제가 있음을 알고 있지만 잘 수정할 수도 있습니다.) POST가 끝나면 리디렉션되므로 뒤로 버튼을 눌러도 아무런 문제가 없습니다. – AP257