weather api
weather api

Reputation: 758

Django form input and action linking

How do you pass form inputs to a form action in Django? I tried this but it's not working

<form action="/search?search_term=q" method="get">
    <input type="text" name="q">
    <input type="submit" value="Search">
</form>

Upvotes: 2

Views: 1687

Answers (4)

shuboy2014
shuboy2014

Reputation: 1370

When you specify a method GET in form, then You don't need to append parameters explicitly in the URL. When the user submits the form your parameter will automatically append in the URL as key-value pairs.

Example

  <form action="search" method="GET">
        <input type="text" name="search_item1">
        <input type="text" name="search_item2">
        <input type="submit" value="Search">
   </form>

When a user hit submit button then your parameter(serach_item1 and serach_item2) will append implicitly in action URL. where keys are input fields name attribute Now your URL looks like

www.example.com/search?search_item1=<serach1>&search_item2=<serach2>

Upvotes: 1

Dwayne Crooks
Dwayne Crooks

Reputation: 2857

I'm assuming you're asking how to link an HTML form to a view function for processing. Note that in Django, we don't call them actions.

Here's the basic form and placeholder for the search results that you'd put in a Django template:

<form action="/search" method="get">
  <input type="text" name="q">
  <input type="submit" value="Search">
</form>

{% if search_results %}
   Display search results here.
{% endif %}

If you type apple into the text field and then click the search button on this form it will make a GET request to /search?q=apple.

You will need to route the request via a URLconf to a view function to do the processing.

The entry in the URLconf may look like the following:

from django.conf.urls import url

from app import views


urlpatterns = [
    # ...
    url(r'^search/$', views.search),
    # ...
]

views.search is the view function that does the processing. Here's a possible implementation:

from django.shortcuts import render


def search(request):
    q = request.GET.get('q')

    if q is None:
        return render(request, 'app/search.html')

    search_results = perform_search(q)
    context = { 'search_results': search_results }

    return render(request, 'app/search.html', context)

Where perform_search is a function you'd need to write to do the actual searching.

Upvotes: 1

RompePC
RompePC

Reputation: 835

shuboy2014 and neverwalkaloner are correct. But a fact they didn't tell you is that you'll pass empty values if you declare no values in that fields. For example:

<form method="GET" action="search/">
  <input type="text" name="search_term">
  <input type="submit" value="Search">
</form>

That would create an URL like http://yourdomain.com/search?search_term=, which could lead to unexpected errors in the View later. You either should do request.GET.get("search_term") to avoid exceptions or modify the submit function of your form to dinamically remove the names of those empty fields.

Upvotes: 0

neverwalkaloner
neverwalkaloner

Reputation: 47374

To pass GET arguments to the page you don't need to specify it in action attribute.

Just do follow:

<form method="GET" action="search/">
    <input type="text" name="search_term" value="{{ request.GET.search_term }}">
    <input type="submit" value="Search">
</form>

this will redirect you to the page http://yourdomain.com/search?search_term=search_text.

Please note I replaced input name attribute to rename GET argument to search_term. Also I added value attribute to display current search text to the input field.

Upvotes: 0

Related Questions