Re: newbie URL problem

52 views
Skip to first unread message

Daniel Roseman

unread,
Jul 26, 2012, 8:28:17 AM7/26/12
to django...@googlegroups.com
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.

Karen Tracey

unread,
Jul 26, 2012, 8:32:02 AM7/26/12
to django...@googlegroups.com
On Thu, Jul 26, 2012 at 12:20 AM, Felipe Sitta <music...@gmail.com> 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'),

Get rid of the (.*) in those url regular expressions. That's matching anything at all, including '/Indianapolis Motor Speedwa', then the y matches \w and presumably the url ends with a slash at the end of the string. urlpatterns are processed in order with first match begin taken, so anything that that starts with 'Track List' and ends with an alphnumeric (\w+) and a slash is matching the first url pattern and being routed to  the trackList view.

(Once you get to the point where the 2nd pattern is matched you'll have a problem since that url captures two parameters for the view but the view itself is only defined to expect one.)

Karen
--
http://tracey.org/kmt/

Karen Tracey

unread,
Jul 26, 2012, 8:40:52 AM7/26/12
to django...@googlegroups.com
On Thu, Jul 26, 2012 at 8:32 AM, Karen Tracey <kmtr...@gmail.com> wrote:
On Thu, Jul 26, 2012 at 12:20 AM, Felipe Sitta <music...@gmail.com> wrote:

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'),

Get rid of the (.*) in those url regular expressions.

And when you do that you'll also need to adjust your track names to have no spaces or explicitly allow space in the country/track names, in general it would be better to slugify your names for use in urls, which will get rid of spaces and other characters that will wind up getting percent-encoded. Also the 2nd url pattern seems to be missing the regex pattern part for the track_name part?

Karen
--
http://tracey.org/kmt/

Felipe Sitta

unread,
Jul 26, 2012, 3:48:14 PM7/26/12
to django...@googlegroups.com
I did what you said and put an third parameter on the track view and it worked. I think it's not an good practice, because is maintaining useless data, but for now its ok, at least the listing is done :D I still have to learn to use those regular expressions, its kinda tricky =/

Anyway, thanks to Karen and Daniel for the help, much appreciated!!!
Reply all
Reply to author
Forward
0 new messages