5

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.

pivotal
  • 736
  • 6
  • 16

1 Answers1

6

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
  • 1,294
  • 8
  • 13
  • 1
    Shouldn't you include the blog_name argument in the url tag? – Mark van Lent Aug 04 '10 at 07:37
  • 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 Aug 04 '10 at 13:59