3

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')
Reiion
  • 813
  • 2
  • 11
  • 27

3 Answers3

6

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.

Piyush Das
  • 376
  • 3
  • 16
  • 1
    For this code to work, you will have to include JQuery. Or have a look at https://stackoverflow.com/questions/9713058/send-post-data-using-xmlhttprequest to get an idea of how to do the same with plain JavaScript. – Piyush Das Nov 24 '17 at 09:55
  • 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 – Reiion Nov 24 '17 at 11:23
  • Yes, in that case, AJAX POST call would be your best option. – Piyush Das Nov 24 '17 at 11:28
  • 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.) – Reiion Nov 24 '17 at 12:13
  • 1
    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. – Piyush Das Nov 24 '17 at 12:19
  • I see. I understand it better now. Reading this now :) https://forum.jquery.com/topic/append-parameter-to-url-during-ajax-call-with-jquery Thank you! – Reiion Nov 24 '17 at 12:34
  • @Reiion next time please indicate correctly what's your real question. In your original post, it says you want to pass parameters not through form post, and without using ajax. That confused people. – Tiny.D Nov 24 '17 at 15:14
  • Yes I will. At the time I posted the question, I really wanted to post without using the form so I can append a certain url and pass parameters. But the parameters are beyond just an "id" or "value" but that would also include probably a json. I apologize. – Reiion Nov 25 '17 at 10:24
5

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.

Ankita Gupta
  • 493
  • 3
  • 13
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'),
]
Tiny.D
  • 6,026
  • 2
  • 11
  • 18