Justin O'Brien
Justin O'Brien

Reputation: 111

Form is not returning contents of search, instead it returns "csrfmiddlewaretoken"?

I have the following search bar (which is included in a Bootstrap Navbar):

<form class="form-inline my-2 my-lg-0 navbar-toggler-right" method = "GET" action = "{% url 'search' %}" >
        {% csrf_token %}
        <input class="form-control mr-sm-2" type="text" placeholder="Search">
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>

The view that handles the 'search' url:

def search(request):
    if request.method == "GET":
        contents_of_search = request.GET
        return HttpResponse(contens_of_search)

The problem is is that whenever something is searched in this search bar it returns "csrfmiddlewaretoken" and the contents of the search. Does anyone know how to fix this?

Upvotes: 0

Views: 24

Answers (1)

Rieljun Liguid
Rieljun Liguid

Reputation: 1531

You need to add a name property in your input text so that you can reference it in request.GET

# html template
<input name="query" class="form-control mr-sm-2" type="text" placeholder="Search">

# view
query = request.GET.get('query')

Upvotes: 1

Related Questions