Re: Django Search via Form Redirect to URL

2014-07-28 Thread Conner DiPaolo


On Monday, July 28, 2014 11:49:12 AM UTC-7, Branko Majic wrote:
>
> 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 
>

Ok I did what you said and played around for a while and got it working! 
Thanks! Another issue I had which I think was the main one (as well as the 
namespace in my input) was that I needed to change my action to link to a 
url which validated my search view. I ended up just using 
/beacon/search/search_query/ but anything instead of search_query would 
have worked. This way Django runs through the search view no matter what 
page you are on. Ironically changing that also implemented my search 
function on the whole site because the form is in my base.html .

Seriously thanks for the help dude!

-Conner DiPaolo 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3d0f0c9a-d4a0-44c7-b17b-62d974f5a7d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django Search via Form Redirect to URL

2014-07-28 Thread Branko Majic
On Mon, 28 Jul 2014 07:41:23 -0700 (PDT)
Conner DiPaolo  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: 
> >
> >  > 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//   . 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: bra...@majic.rs
Please use only Free formats when sending attachments to me.

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


signature.asc
Description: PGP signature


Re: Django Search via Form Redirect to URL

2014-07-28 Thread Conner DiPaolo


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: 
>
>  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//   . 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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/25167e59-eefb-4582-80fd-a31546456156%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django Search via Form Redirect to URL

2014-07-28 Thread Branko Majic
On Sun, 27 Jul 2014 15:11:20 -0700 (PDT)
Conner DiPaolo  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):
> 
>  method='POST'>
> 
> {% csrf_token %}
> 
> 
> Submit
> 

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:



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: bra...@majic.rs
Please use only Free formats when sending attachments to me.

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


signature.asc
Description: PGP signature


Django Search via Form Redirect to URL

2014-07-27 Thread Conner DiPaolo


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):



{% csrf_token %}


Submit


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/952d7d70-4f65-4283-bcd0-45e4385ae672%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.