Monday, July 28, 2014

Re: Django Search via Form Redirect to URL

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

iQIcBAEBAgAGBQJT1prkAAoJECtGpaYHamaxFmcQAKN9HQUFSeOVXUgXsckUjhU4
KyDMkMkPR82rbe3rQl3xpCceRRhIk27cp1iYQtlSqG8b6nJJuOPHPJTphwL8yqur
fIbCLfyLb36+9UEes/6FK/yuXH2D7OUpmVRPVAl9kCWgmIB0V1td7FBD4/CUNR0D
zcV0Sw1m9XMZmt0vnXHQ4CJZdfiJsQ06JdvHDPCQnw7YgTEWeC+GorQlNr+CUBvK
KcZPKz584Men7ABW57oD1GoQI/eD9f58btSeznr47fvj04ydPrZZe4gyssgxOnkz
wIQwPEQscTaVLx01oyP2LqXZy82Cn89gN5Pr8AeKgxopTz6mWLDU8Ffc1rurHZEn
zCsbeosaWZnRONjzgRApDorSBY1ptJgDxG/MHsck1fEOyTJXyWi1oJWJ93bUNWRz
UrJdcyoU2h89sn3xoLlnjzNl3M3bvjiLw36biwfxKBl6HQZ6M7au+VpRxCYNbidn
3hBos1yz4y5uDj1rcevqi6lPb6Jua2vCZVlRsMo5zurcmCsu9LzSWMOMpJvOGt5Y
FrJ1ncfnZT6o1T7TMy8n5Gq4ce7ULQcdDH/ziXc0el+0x6Cq8SAEtXbnzRE1IQtZ
wwkKwsE4bG4IYYqqlcNMdtISXif7VWsgihhPWf8hcwEpa7IqvA4+Hr1XCOEa09Co
GAbawqAj3CxTOV9GrKd4
=im5u
-----END PGP SIGNATURE-----
On Mon, 28 Jul 2014 07:41:23 -0700 (PDT)
Conner DiPaolo <cdipaolo96@gmail.com> wrote:

>
>
> On Monday, July 28, 2014 4:42:07 AM UTC-7, Branko Majic wrote:
> >
> >
> > 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).
>
>
> Thanks for the response. I tried putting a name into the text input field
> but nothing changes- I am still redirected to /project/search/ and not
> /project/search/<query>/ . I have also tried to just use the default
> Django form link template tag with {{ form.search_string }} with the same
> result, which leads me to think that the issue has to do with maybe the
> submit button? I'm not sure, though...
>
> I agree that POST is probably the less 'correct' thing to do here, the
> perfect example for using GET is ironically (in the Django docs themselves)
> a search bar. That said, I get so confused and frustrated when I have to
> use forms that I want to have a working prototype before I change it as
> most of the form tutorials out there use POST because they change models or
> something.
>
> Seriously thanks for the response!
> Conner DiPaolo
>

I have tried hacking your code on top of one of my search pages, and
definitively managed to get it working once I added the name for the
input.

Without the name, the search view would otherwise just fall through and
I'd get something in the lines of the following error:

The view project.views.search didn't return an HttpResponse object.

I'd suggest maybe putting in a couple of debug print messages in your
view to figure out where your code ends-up in the end.

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