Monday, July 28, 2014

Re: Django Search via Form Redirect to URL

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBAgAGBQJT1jbQAAoJECtGpaYHamaxstYQAIYZ3Hsq+fvP2Xxv172Fr7KT
JXbqjfKr5p58Cqxkz4H7dJDkyQGjtbM/sQOQB6dIY82I+Rr3wsj2cQ/LfoqKighh
iT7+oQr5zGGKEv+fSLQkKG9VmbqRUFSS0vRq2iPJMbFVQX0ruhLtZ6W+IcwEg2/O
pGiAguOTWl5MPUwDxUeVe0uyYtWkYWrzkChMI8aenFXqiQSPbpANlc3/eEFWwaE3
vzwl5HdpgU1J9ocODKKZUHyO7LR0fiYIwAxfEAJuCGabUZFczdpl6Pg50YkUzcam
spfTK5AI1SaPtglHfv5rJ0mfmvBHThsizzju8kzt5Rv2ncgCEvCMFEGAj8ItZfyJ
+qHk90LzYb5lUPrFclBm8+S4GUDf056t0H3FRcCthJLwIIwU/0QHuaVy8q8apYDC
Vz8qu6DHSTnG3js1wWnlg5ilvVpay1oYfrv2H22NZd3IC1OYZoD+7CccbmQOzeeQ
dbX/HnIHWpT3ELOsum1uMcUPsqmTplPftfCVg2Hut1hOoR8aL8Gst/uNnIPHnKhS
VA/3jMoYJMtcv03whacaNn3E5qEjlctjuhfZORa2/bNptbsQ7AxUSMT+ZAh5eI7m
jca7hcv5A47kfukseEBuWfO59j+8Gbr8aPRfYRMj1+kGG/1VPbCIxBf6MzWh5rG/
2AHCfgZugNvb1dx4DLM6
=jshT
-----END PGP SIGNATURE-----
On Sun, 27 Jul 2014 15:11:20 -0700 (PDT)
Conner DiPaolo <cdipaolo96@gmail.com> wrote:

>
>
> I already have a valid search function mapped in my Django project, you
> type in a url '/project/search//' and it responds the corresponding search
> results on a page. I want to have a search box in my nav-bar that was
> created in my base.html and have users type in a query and it will redirect
> them to the search url. When I type into the search bar and click submit,
> however, the I am just directed to '/project/search/' with no relation to
> the query. How do I connect the form to redirect to the URL, *properly*??
>
> Thanks in advance.
>
> Here is my view definition and form class:
>
> class SearchForm(forms.Form):
> search_string = forms.CharField(max_length=100)def search(request, search_query):
> if request.method == 'GET':
> form = SearchForm()
> context = RequestContext(request)
> search_string = search_query.replace('_',' ')
> search_terms = search_query.split('_')
> search_results = Article.objects.all()
> for term in search_terms:
> search_results = search_results.filter(article__icontains=term)
>
> context_dict = {
> 'search_string':search_string,
> 'search_results':search_results,
> 'form':form,
> }
>
>
> if request.method == 'POST':
> form = SearchForm(request.POST)
> if form.is_valid():
> search_string = form.data['search_string']
> search_query = search_string.replace(' ','_')
> ###return HttpResponseRedirect(reverse('search', args=(search_query,)))
> search_url = '/beacon/search/' + search_query + '/'
> return HttpResponseRedirect(search_url)
> else:
> return render_to_response('search.html', context_dict, context)
>
> My base form html is (yes it is from bootstrap):
>
> <form action='/beacon/search/' class="navbar-form navbar-right" role="search" method='POST'>
> <div class="form-group">
> {% csrf_token %}
> <input type="text" class="form-control" placeholder="Search Article Text">
> </div>
> <button type="submit" class="btn btn-default">Submit</button></form>
>

In your search method, if the request method was POST, you are trying
to retrieve submitted data via key 'search_string'. However, if you
look at your HTML form, there does not seem to be any kind of input
field called 'search_string'.

Try adding name="search_string" parameter to the input element, for
example:

<input type="text" name="search_string" class="form-control" placeholder="Search Article Text">

On a side-note, why are you even using POST method for a search? I
_think_ that GET should be sufficient, and probably more correct thing
to use, unless your searches are actually altering data (which could be
considered a bad practice).

Best regards


--
Branko Majic
Jabber: branko@majic.rs
Please use only Free formats when sending attachments to me.

Бранко Мајић
Џабер: branko@majic.rs
Молим вас да додатке шаљете искључиво у слободним форматима.

No comments:

Post a Comment