My urls.py has an entry:
urlpatterns = [
url(r'^results/(?P<query>).+', views.ResultsView.as_view(), name="results"),
]
which matches to the corresponding Class Based View:
class ResultsView(TemplateView):
template_name = os.path.join(APPNAME, "results.html")
def dispatch(self, request, *args, **kwargs):
query = kwargs['query']
print("HERE: " + str(json.dumps(kwargs, indent=1)))
print(self.kwargs['query'])
print(self.kwargs.get('query'))
print(kwargs['query'])
print(kwargs.get('query'))
if query is None:
return redirect('/')
return super(ResultsView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(ResultsView, self).get_context_data(**kwargs)
print("HERE: " + str(json.dumps(kwargs, indent=1)))
print(self.kwargs['query'])
print(self.kwargs.get('query'))
print(kwargs['query'])
print(kwargs.get('query'))
...
# This is just here to test if 'query' is set
def get(self, request, query):
print(query)
I'm trying to get the value of the query variable that is set in urls.py. However, after trying various solutions I found on other SO posts (as you can see from all the print statements), nothing is working.
I'm fairly sure my urls.py is set up properly, because the request resolves to the correct page (results/), but all attempts to print the query entry of the dict return an empty string, and json.dumps(kwargs, indent=1)) prints this:
HERE: {
"query": ""
}
What am I doing wrong?
http://127.0.0.1:8000/results/australiawhereaustraliais the query term set by the user in the search bar.url(r'^results/(?P<query>[\w.-]+)', views.ResultsView.as_view(), name="results"),