2

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?

6
  • can you show an actual url?? like with a query you are passing Commented Sep 12, 2017 at 4:56
  • Sure, locally, a url with a query might look like this: http://127.0.0.1:8000/results/australia where australia is the query term set by the user in the search bar. Commented Sep 12, 2017 at 5:00
  • 1
    you are making a mistake, the search term will be appended to the url and not like that one, 127.0.0.1:8000/results/?q=australia, like this. so you dont need that parameter part, in the view, do request.GET.get('q') and you will get the search term, and dont forget to remove the parameter from the url Commented Sep 12, 2017 at 5:04
  • 1
    Looks like you missed pattern part in url. Try to change it to this: url(r'^results/(?P<query>[\w.-]+)', views.ResultsView.as_view(), name="results"), Commented Sep 12, 2017 at 5:14
  • 1
    Thanks @neverwalkaloner. That looks like it works to me. My regex is a little rusty, so I must have misunderstood what mine would match. If you want to write it as an answer, I'll mark it as correct. Commented Sep 12, 2017 at 5:26

1 Answer 1

3

Just copy my comment as an answer. You missed pattern part in url's regular expression. Try to change url to this:

url(r'^results/(?P<query>[\w.-]+)', views.ResultsView.as_view(), name="results")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.