0

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.

datasci
  • 1,019
  • 2
  • 12
  • 29
  • Normally search parameters are passed through a query string: `http://www.example.com/results/?first_name=john&last_name=doe` and then you can access them in your view using `request.GET`. So no need to specify another url. – Germano Jul 03 '14 at 13:25
  • Sure, that works. So, there is nothing special that I can do to show the full path of the url instead of just '/results'? I suppose it's not a huge issue but visually is nice. – datasci Jul 03 '14 at 13:40
  • If first_name and last_name are always supplied, you can use them as url parameters url(r'^results/(?P\w+/(?P\w+))',views.results, name='results'), – Alvaro Jul 03 '14 at 13:50

2 Answers2

0

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.).

argaen
  • 4,145
  • 24
  • 28
  • I get an error saying I'm requesting 'http://www.example.com/results' and no url patterns match. In this solution, do I need to adjust my HttpResponse to request the same regex? Currently, my template is at 'results.html', which is what I'm requesting in my HttpResponse. – datasci Jul 03 '14 at 14:09
  • this because you don't have to request this url. You have to request example.com/results/first_name/last_name. This request will call your view and your view will pass the data to your template to render it. – argaen Jul 03 '14 at 14:24
0

Thanks for all of your suggestions! Regex is extremely powerful; I'll need to spend a couple of weeks learning it more closely. Using your suggestions, I was able to get it to work using the following:

urls.py

url(r'^results/(?P<first_name>\w)+(?P<last_name>\w)',views.results, name='results')
...

views.py

def search(request):
...
return HttpResponseRedirect('results/' + first_name + '+' + last_name)

def results(request,first_name,last_name):
...

Thanks again!

datasci
  • 1,019
  • 2
  • 12
  • 29