Let's say I have the following form:
<form id="search-form" class="form-horizontal" method="GET" action="/results/" role="form">
<input type="text" class="form-control" id="query" name="query">
<button type="submit" form="search-form" class="btn btn-primary">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
</button>
</form>
Currently, when the form is submitted, the url contains the query as a parameter, like so: www.<my-domain>.com/results?query=<some-query>
. However, for SEO purposes, I want to convert this to a 'clean url', which would look more like this: www.<my-domain>.com/results/<some-query>
.
I've made the following modifications to my urls.py:
urlpatterns = [
...
url(r'^results/(?P<query>[a-zA-Z ]+)$', views.ResultsView.as_view(), name="results"),
...
]
So I should be able to grab the query value in my ResultsView like this:
class ResultsView(TemplateView):
def dispatch(self, request, *args, **kwargs):
query = kwargs.get('query')
How can I submit the form so it'll match with the corresponding url pattern? I've tried fiddling with the form action passing the input id to it, like this: action="/results/query/"
, but obviously, it thinks query
is a literal string, and doesn't replace it with the value of the input.