How to implement search view for list view?

Django (web framework): How can I implement a urlpattern that passes a list of strings to the view?

  • For example: I have a url like this: http://mysite/StringA/StringB/StringN And I want to always process this with the same view, but I would like the view to receive a list ['StringA', 'StringB', 'StringN'] for processing. Is there an easy way to setup http://urls.py to pass this information along, or would it be easier to just setup a regex to capture any URL and have the view parse the URL afterwards?

  • Answer:

    I have been experimenting and there are several ways to do this, the summary is that you just need to match all characters and front slashes. If you want your URLs to include numbers or other characters, you need to include that too. In your http://urls.py (preferably after your other routes, since this stands to intercept a lot): 1(r'^/([A-Za-z/]+)','home.views.blarg'), And assuming you have a controller called home, in http://views.py you need a method like this: 1def blarg(request, arguments): Here, arguments will contain a string of the fully called path, excluding the base of home. As per the question, you can split it into a list like so: 1arglist = arguments.split('/')

John Clover at Quora Visit the source

Was this solution helpful to you?

Other answers

If you want to catch any URL that matches 3 levels then: url(r'^(?P<stringA>.*)/(?P<stringB>.*)/(?P<stringN>.*)/$', 'views.myview'), def myview(request, stringA, stringB, stringN):     return render(request, 'template.html') where the actual values are in the view parameters. This will match both: http://example.com/one/two/three/ http://example.com/a/b/c/ OTOH if you want to match verbatim just do: url(r'^stringA/stringB/stringN/$', 'views.myview'), def myview(request):     return render(request, 'template.html') If you need to match another string sequence just define a new URL pattern in http://urls.py and point it to the same (or another) view function.

Alexander Todorov

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.