A few things are not very good here ...
1) The recommended thing after POST is redirection. This avoids the pop-up message that you are re-adding the form using the back button.
2) You do not need to say if request.method == 'POST'
, just if request.POST
. This makes it easier to read code.
3) The view usually looks something like this:
def myview(request): # Some set up operations if request.POST: form=MyForm(request.POST) if form.is_valid(): # some other operations and model save if any # redirect to results page form=MyForm() #render your form template
This does not mean that there cannot be much simpler and more complex representations. But this is the essence of the view: if the request processes the form and redirects it; if the request receives a visualization of the form.
I do not know why you are getting unicode error. I can only think that this is due to one of your models that you do not provide. The error, as spookylukey mentioned in his comment, is most likely due to the fact that you are sending a string instead of a dict to the original parameter.
I really recommend the django documentation in particular the tutorial. but there is also a very nice Django Book .
All that said, I think you need something like:
def search(request, q=None): if request.POST: form = SearchForm(request.POST) if form.is_valid(): q = form.cleaned_data['q'] url=reverse('search_results', args=(q,)) return HttpResponseRedirect(url) if q is None: form = SearchForm() else: form = SearchForm(initial={'q': q}) return render_to_response('things/search.html', { 'form': form, })
Note that the original parameter is a type of field values ββfor your form.
Hope this helps.
cethegeek
source share