Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Several things are not good here...</p> <p>1) The recommended thing after a POST is to redirect. This avoids the infamous popup saying that you are resubmitting the form when using the back button.</p> <p>2) You don't need to say <code>if request.method == 'POST'</code>, just <code>if request.POST</code>. That makes your code easier to read.</p> <p>3) The view generally looks something like:</p> <pre><code>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 </code></pre> <p>That is not to say that there can't be much simpler and much more complicated views. But that is the gist of a view: if request is post process the form and redirect; if request is get render the form.</p> <p><strike>I don't know why you are getting an unicode error. I can only think that it is related to one of your models that you don't provide.</strike> The error, as spookylukey mentions is in his comment, most likely is caused by you submitting a string instead of a dict to the initial parameter.</p> <p>I really recommend the django documentation, in <a href="http://docs.djangoproject.com/en/dev/intro/tutorial01/#intro-tutorial01" rel="noreferrer">particular the tutorial.</a>, but there is also the very nice <a href="http://www.djangobook.com/en/2.0/" rel="noreferrer">Django Book</a>.</p> <p>All that said, I think you want something like:</p> <pre><code>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, }) </code></pre> <p>Notice that the parameter to initial is a dict of the field values of your form.</p> <p>Hope that helps. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload