Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse django feed url?

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.

like image 996
pivotal Avatar asked Aug 04 '10 03:08

pivotal


1 Answers

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.

like image 52
Joseph Spiros Avatar answered Sep 21 '22 19:09

Joseph Spiros