views:

42

answers:

1

I've been searching for hours to try and figure this out, and it seems like no one has ever put an example online - I've just created a Django 1.2 rss feed view object and attached it to a url. When I visit the url, everything works great, so I know my implementation of the feed class is OK.

The hitch is, I can't figure out how to link to the url in my template. I could just hard code it, but I would much rather use {% url %}

I've tried passing the full path like so:

{% url app_name.lib.feeds.LatestPosts blog_name=name %}

And I get nothing. I've been searching and it seems like everyone else has a solution so obvious it's not worth posting online. Have I just been up too long?

Here is the relevent url pattern:

from app.lib.feeds import LatestPosts

urlpatterns = patterns('app.blog.views',
    (r'^rss/(?P<blog_name>[A-Za-z0-9]+)/$', LatestPosts()),
    #snip...
)

Thanks for your help.

+4  A: 

You can name your url pattern, which requires the use of the url helper function:

from django.conf.urls.defaults import url, patterns

urlpatterns = patterns('app.blog.views',
    url(r'^rss/(?P<blog_name>[A-Za-z0-9]+)/$', LatestPosts(), name='latest-posts'),
    #snip...
)

Then, you can simply use {% url latest-posts blog_name="myblog" %} in your template.

Joseph Spiros
Shouldn't you include the blog_name argument in the url tag?
Mark van Lent
Aww crap I guess I was up a bit too late. I tried naming it, but failed on the url helper function. With the addition of the parameter, this works. Thank you!
pivotal
@Mark van Lent: You're right, I updated the answer.
Joseph Spiros