[Fixed]-Django how to properly submit form and handle request?

1👍

That error:

Reverse for 'search' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['search/(?P<trim>\\d+)/$']

means that you didn’t give the id parameter (you called it trim). It should be part of the arguments or keyword arguments.

I assume that’s because there is no trim object in the template here:

<form action="{% url 'search:search' trim.id %}"

Is that template rendered via a DetailView? If that’s the case the following would probably work:

<form action="{% url 'search:search' object.id %}"

0👍

The solution to the problem I was having lied in my url patterns. By taking out the ID portion of the search URL, and changing the view as such:

def search(request):

    trim = request.GET['selectedTrim']
    selectedTrim = get_object_or_404(Trim, id=trim)
    context = {'trim': selectedTrim}
    return render(request, 'search/detail.html', context)

and my form action to:

action="{% url 'search:search' %}"

I was able to successfully pass the selected trim to the view via request method, and not through the url regex variable, which was causing the error due to the selected trim ID not actually existing via context provided to the template (I think).

👤sidp

Leave a comment