I'm new to Django, and I'm having trouble understanding URL patterns. When a user visits the index page of my website (http://www.example.com), they have the ability to conduct a search. They input a first name in one box, and a last name in another, and then click a search button. The user's search returns information on a results page (http://www.example.com/results). Everything works perfectly when I use the following pattern:
urlpatterns = patterns('',
...
url(r'^results',views.results, name='results'),
...
)
However, instead of a rending a '/results' URL for every single search, how would I render a URL like this that captures the actual query:
http://www.example.com/results/<first_name>'+'<last_name>/
'first_name' and 'last_name' are request.session[] variables stored in the view. I'm sure that this is a very simple problem, but given that I'm new to all of this I was hoping someone could help me understand how this works.
I appreciate the help.
By naming groups in your url pattern. Of course, this is complicated if you just concatenate first_name and last_name because then you can't know how to distinguish which is anyone. You should concatenate like this first_name+"/"+last_name
and then your url pattern should look like this:
url(r'^results/(?P<first_name>[\w-]+)/(?P<last_name>[\w-]+)$',views.results, name='results'),
Then, your results view, should accept two parameters:
def results(request, first_name, last_name):
Hope you are familiar with regexp. And, ofc you need to control yourself if first_name and last_name are not correct (i.e. void, odd values, etc.).