我正在使用 Django Feeds Framework,它非常好、非常直观且易于使用。但是,我认为在 HTML 中创建提要链接时存在问题。
例如:
<link rel="alternate" type="application/rss+xml" title="{{ feed_title }}" href="{{ url_of_feed }}" />
链接的 HREF
属性可以很容易地找到,只需使用 reverse()
但是,TITLE
属性呢?模板引擎应该在哪里寻找这个?更重要的是,如果 feed 是动态构建的并且标题取决于参数(例如 这个)?
我无法想出一个对我来说“似乎”干燥的解决方案...我能想到的就是使用上下文处理器或模板标签,但是当上下文处理器/模板标签必须找到参数时,它会变得混乱构造 Feed 类,并在编写此内容时我意识到我什至不知道如何在视图中自己创建 Feed 实例。
如果我把所有这些逻辑都放在视图中,它就不仅仅是一个视图。此外,TITLE
的值将位于视图和提要中。
I'm using the Django Feeds Framework and it's really nice, very intuitive and easy to use. But, I think there is a problem when creating links to feeds in HTML.
For example:
<link rel="alternate" type="application/rss+xml" title="{{ feed_title }}" href="{{ url_of_feed }}" />
Link's HREF
attribute can be easily found out, just use reverse()
But, what about the TITLE
attribute? Where the template engine should look for this? Even more, what if the feed is build up dinamically and the title depends on parameters (like this)?
I can't come up with a solution that "seems" DRY to me... All that I can come up with is using context processors o template tags, but it gets messy when the context procesor/template tag has to find parameters to construct the Feed class, and writing this I realize I don't even know how to create a Feed instance myself within the view.
If I put all this logic in the view, it would not be just one view. Also, the value for TITLE
would be in the view AND in the feed.
发布评论
评论(2)
只是一个猜测(因为我还没有在我的 django 应用程序中使用 feeds),但是您可以使用 feed 对象为 feed 添加一个特殊的 template_context ,并在您的 base.html 中使用它。
Just a guess (as I have not used feeds yet in my django app), but you could add a special template_context for your feed with your feed object and use it in your base.html.
我对这个解决方案并不完全满意,它可能会破坏使用
Request
的提要,并且依赖于一种神奇的方法。事情是这样的:我从
{% url %}
模板标记的代码开始,然后在获取 feed 的 URL 后,使用resolve()
来获取Feed子类实例,然后获取需要的属性。注意事项
request
上下文处理器,因为如果上下文中不存在,则传递None
。I'm not fully satisfied with this solution, it may break feeds using
Request
and depends on a magic method. There it goes:I'm starting with the code of the
{% url %}
template tag, and then, after obtaining the feed's URL, useresolve()
to get the Feed subclass instance, and then get the needed attributes.Caveats
request
context processor must be configured, sinceNone
is passed if it isn't present in the context.