python - Django: Class Based View accessing URL variables -


my urls.py has entry:

urlpatterns = [      url(r'^results/(?p<query>).+', views.resultsview.as_view(), name="results"), ] 

which matches 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 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'))         ...      # here test if 'query' set      def get(self, request, query):         print(query) 

i'm trying value of query variable set in urls.py. however, after trying various solutions found on other posts (as can see print statements), nothing working.

i'm sure urls.py set properly, because request resolves correct page (results/), attempts print query entry of dict return empty string, , json.dumps(kwargs, indent=1)) prints this:

here: {  "query": "" } 

what doing wrong?

just copy comment answer. missed pattern part in url's regular expression. try change url this:

url(r'^results/(?p<query>[\w.-]+)', views.resultsview.as_view(), name="results") 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -