Passing form GET data in URL tag

I’ve been looking for the right syntax to do this and haven’t found something that works.

search_string is the parameter to be passed to the urls.py and views.py but i’m not sure about the value syntax to pass that string properly. Thanks

<form action="{% url 'searchnet:search' search_string=request.GET.get('search') %}" class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3">
        <input type="search" id="search_bar" name="search" method="GET" class="form-control" placeholder="Search.. Anything, Anytime, Anyone, Anywhere, Anyhow" aria-label="Search"> 
</form>

and get this error:

Could not parse the remainder: ‘(‘search’)’ from ‘request.GET.get(‘search’)’

I’ve seen many questions about this error while researching it, but can’t figure out how it pertains to my case. I can’t get anything to work. Can one of you folks see where I’m in error here? thanks

The url in the template tag is rendered on the server at the time the page is created. It has no access to any input fields.

I believe that that “search” field is going to be submitted as a query variable on the URL. If so, then you do not specify it being used in the url tag. (It also means that your URL would not have the variable defined on it.) You would do that request.GET... in the body of your view to retrieve that query variable.

ok, I’ve tried this too →

urls.py

app_name = 'searchnet'

urlpatterns = [
    path('', views.index, name='index'),

    # This is the same as vote from the tutorial
    path('search/<str:search_string>/', views.search, name='search'),

    path('results/<str:search_string>/', views.results, name='results'),
]

views.py

def search(request):
    # This will eventually do some things

    search_string = request.GET('search_bar')

    return HttpResponseRedirect(reverse('searchnet:results', args=(search_string,)))


def results(request, search_string):
    # This view should pass on the redirect
    template = 'searchnet/results.html'
    # search_string = str(request.GET('results'))
    context = {
        "search_string": search_string,
        "fringe_string": search_string[::-1],
    }
    return render(request, template, context)

new form

 <form action="{% url 'searchnet:search' %}" method="GET" class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3">
        <input type="search" name="search_bar" class="form-control" placeholder="Search.. Anything, Anytime, Anyone, Anywhere, Anyhow" aria-label="Search">
   </form>

I want to simply pass the search string into the search view (which will eventually process it and won’t ever be seen by the user) and then sends it to the results view which will, obviously, display the results.

This is the same pattern used in the tutorial except the tutorial passed a question object in the form action field. I’m simply trying to pass a string. What is wrong here?

The above setup gives this error now →

Reverse for ‘search’ with no arguments not found. 1 pattern(s) tried: [‘search/(?P<search_string>[^/]+)/\Z’]

Seriously, I’m not new to python. Why is passing a simple GET string so difficult, it’s been on and off for 2 days I’ve been working on this

Not quite right.

The url would just be 'search/'. You are not adding this field into the url.

You don’t need this at all. This only confuses the situation.

Really close - you were closer with your original code:

This doesn’t help:

Your search view can do the search and return the results directly.

Nothing is “wrong” - it’s just the difference between how form data is passed to Django via GET instead of POST.

Had you done this as a traditional form with a POST, it would have been a lot closer to the tutorial - although I’m not entirely sure which part of the tutorial you’re comparing it to. There may be other factors in play - I’m not sure.

Ok, I get it. I can now see the nuances between POST and GET when it comes to passing them in django. The url pattern with str:search_string isn’t need with GET because it does it automatically, not so with POST.
I see that clearly now.
I’ve finally got this working, thank you.
For anyone else who is having a similar problem and finds this thread the solution is exactly how Ken said it →

urls.py

app_name = 'searchnet'

urlpatterns = [
    path('', views.index, name='index'),
    path('search/', views.search, name='search'),

    # path('results/<str:search_string>/', views.results, name='results'),
]

views.py

def search(request):
    # This will eventually do some things

    search_string = request.GET.get('search_bar')
    template = 'searchnet/results.html'
    context = {
        "location": location,
        "weather": weather,
        "search_string": search_string,
        "fringe_string": search_string[::-1],
    }

    # return HttpResponseRedirect(reverse('searchnet:results', args=(search_string,)))
    return render(request, template, context)

(results() was completely removed from views)
And the form action field is →

<form action="{% url 'searchnet:search' %}" method="GET" class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3">
        <input type="search" name="search_bar" class="form-control" placeholder="Search.. Anything, Anytime, Anyone, Anywhere, Anyhow" aria-label="Search">
</form>