简体   繁体   English

如何反转django feed网址?

[英]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. 我一直在寻找时间来尝试解决这个问题,而且似乎没有人在网上发布过示例-我刚刚创建了Django 1.2 rss feed视图对象并将其附加到url。 When I visit the url, everything works great, so I know my implementation of the feed class is OK. 当我访问url时,一切正常,因此我知道feed类的实现是可以的。

The hitch is, I can't figure out how to link to the url in my template. 麻烦的是,我不知道如何链接到模板中的URL。 I could just hard code it, but I would much rather use {% url %} 我可以对其进行硬编码,但是我更愿意使用{%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. 谢谢你的帮助。

You can name your url pattern , which requires the use of the url helper function: 您可以命名url模式 ,这需要使用url helper函数:

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. 然后,您只需在模板中使用{% url latest-posts blog_name="myblog" %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM