On Thursday, 26 July 2012 05:20:34 UTC+1, Felipe Sitta wrote: > > I'm new to programming and I'm designing an demo website to get used to > django, web and database stuff. It's about racetracks. > > The structure is the following: > There's a menu with a link "Track List", it leads to a page with an > country list each one as a link. > Each link leads to an list with the racetracks from the respective > country, and then clicking on a track name leads to a page with > informations. > > So the links will be looking, for example, like this: > > localhost/Track List/United States/Indianapolis Motor Speedway > > > Everything's fine until "United States" when I click the track name it > ignores the view function that creates the track information layout, > instead of this, it recalls the country list view (which pops an error > since data is different on the layout) > > I messed up with the code and figured that the problem is with the urls > patterns. Here are the codes: > > the url patterns from urls.py > > url(r'^Track > List/(?P<countryList_countryName>(.*)\w+)/$','tracks.views.trackList'), > url(r'^Track > List/(?P<countryList_countryName>(.*)\w+)/(?P<track_name>)/$','tracks.views.track'), > > > the views > > def trackList(request, countryList_countryName): > > trackList=Track.objects.filter(country__startswith=countryList_countryName).order_by('name') > t=loader.get_template('trackList.html') > c=Context({'tracks':trackList}) > return HttpResponse(t.render(c)) > > def track(request, track_name): > track=Track.objects.get(name=track_name) > t=loader.get_template('track.html') > c=Context({'track':track}) > return HttpResponse(t.render(c)) > > > I'd like to know what should I put on the url pattern to maintain the > current url and seek for the next part, the (?P<track_name>) fragment. > > Any help is welcome, thanks to all the community! >
There are two things wrong here. First, your `track` URL is capturing a `countryList_countryName` parameter in addition to the `track_name` one, but the view does not accept it. You should have both parameters in the view definition, or just one in the url conf. Secondly, Django won't actually ever get to the track view, because the first URL will match both `/United States/` and `/United States/Indianopolis Motor Trackway/', because your regex is greedy. An easy way to solve this is to put the urls in the other order, so that it attempts to match the one with country+track before trying the country-only one. As an aside, you should avoid using spaces in URLs, and it's also best to use all lower case. Make the hard-coded bit something like '/track_list/' and use slugs instead of full names for the countries and tracks - `/track_list/united-states/indianapolis-motor-speedway/`. -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/jQ9aUJrER-AJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.