1
# my .views
def search_view(request):
    query = request.GET.get('q', None)
    print(query)
    context = {"query": query}
    return render(request, 'view.html', context)
# .urls
path('search/', search_view)

# my view.html
{% if query %}

        <p>Your query {{ query }}</p>
        {% else %}

        <form method="GET" action='/search/'>
            <input type="search" name='q' placeholder="Search" aria-label="Search">
            <button type="submit">Search</button>
        </form>

        {% endif %}

I'm new to Django and I'm following the docs, I looked over and over and seems right to me but I keep getting query=None. When I input a value in the search bar goes to http://127.0.0.1:8000/search/?q=value, but the query is always None. Please help a noob getting started.

Brocante
  • 21
  • 1
  • Is that your actual code? It looks ok to me. – Alasdair Feb 20 '20 at 16:46
  • I copied and tested the same code and works, @Brocante. – Tané Feb 20 '20 at 16:59
  • I think there was some conflicts in url's paths because pressing the search button didn't send me to the 'view.html' template, works now but still have no exact idea about where the error was – Brocante Feb 20 '20 at 17:04

2 Answers2

0

welcome to SO! Have you tried this? From reading this, it seems like you are having some issues returning your value back from the form to your function.

Roah
  • 1
  • 3
  • I checked it but was not helpful, but I got where the problem is, the url doesn't load the right template – Brocante Feb 20 '20 at 16:48
0

I don't know what are looking at at the end, but that is not a good job. did you try an < a > tag too?

< a href="search/?q=sth">

mh-firouzjah
  • 834
  • 1
  • 6
  • 15