4

I would usually do this in ajax I think.

But if for example I have a button and a search input box that will have a query value, how can I pass the search value (and possibly multiple values/parameters that is not obtained through a form field) to Django views on button click to the url?

HTML

 <div> <input id="search" name="search" type="text" class="query-search" placeholder="Search..."> <a class="btn btn-primary btn-lg btn-space col-sm-3" href="{% url 'routes_view' %}">View Routes</a> </div> 

Views

def view_detail(request): ... <How to get values from HTML without using ajax? Or its the only way?> 

URLs

url(r'^view/', views.view_detail, name='view_detail') 

3 Answers 3

7

Understanding from the previous answer, if you do not want the search values to be sent as URL parameters, you can send them via an AJAX POST call and handle that in the view, like this:

<div> <input id="search" name="search" type="text" class="query-search" placeholder="Search..."> <input onclick="setGetParameter()" type="button" class="btn btn-primary" value = "View Details"/> </div> <script> function setGetParameter(){ var search_word = $("#search").val(); $.ajax({ type: "POST", url: {% url 'view_detail' %}, data: search_word, success: function(result){ alert("Success"); } }); } </script> 

Your view should look something like this.

def search_view(request): if request.method == "POST": search_word = request.POST['data'] 

URL configurations will remain the way other answers describe them.

Sign up to request clarification or add additional context in comments.

8 Comments

For this code to work, you will have to include JQuery. Or have a look at stackoverflow.com/questions/9713058/… to get an idea of how to do the same with plain JavaScript.
yes, I think ajax would be the only way other than directly passing it to the url. I have objects that I didn't and can't pass thru the url like json
Yes, in that case, AJAX POST call would be your best option.
Can I ask though, I'd usually do ajax call also, however, during the times I use it, it doesn't append to the url. (It goes to the correct url to view though ofcourse.)
AJAX is used to make calls with different methods like GET and POST and make changes in parts of your html document on the events of success or failure. Now, even for a GET method you will have to manually create the URL by appending your parameters. AJAX won't do that for you.
|
7

You can create a HTML form and can submit the form using post method if you do not want to pass values in url.

<div> <form action="{% url 'view_detail' %}" method="post"> {% csrf_token %} <input id="search" name="search" type="text" class="query-search" placeholder="Search..."> <input class="btn btn-primary" type="submit" value="submit"> </form> </div> 

And then in your view you can fetch the data as request.POST.get('search'):

def view_detail(request): searchWord = request.POST.get('search','') return HttpResponse(searchWord) 

In the same way you can pass multiple parameters.

1 Comment

add {% csrf_token %} to the form.
3

You can make a button works like a link in your templates, and add the js function on the onclick event, then get the search input value and add the search parameter to the url window.location.href:

<div> <input id="search" name="search" type="text" class="query-search" placeholder="Search..."> <input onclick="setGetParameter()" type="button" class="btn btn-primary" value = "View Details"/> </div> <script> function setGetParameter(){ searchWord = document.getElementById('search').value; window.location.href = "{% url 'view_detail' %}" + "?search="+searchWord; } </script> 

In the views, you can use request.GET.get('search') to get the parameters (search content) you input from the templates, like this:

def view_detail(request): searchWord = request.GET.get('search') return HttpResponse(searchWord) 

BTW, the urlpatterns is look like this:

urlpatterns = [ url(r'^$', views.index, name='index'), url(r'^view$', views.view_detail, name='view_detail'), ] 

2 Comments

thanks! But what if I would like to pass multiple values in the views without passing it to the url?
if it is not a form value to post, you have to pass the parameters to the url.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.