在django联合中获取请求参数?

时间:2015-07-04 01:15:50

标签: python django feed syndication

这是一个包含超级秘密Feed的哈希的网址:

  

http://127.0.0.1:8000/something/feed/12e8e59187c328fbe5c48452babf769c/

我正在尝试捕获并发送'12e8e59187c328fbe5c48452babf769c'变量feed_hash(充当slug以检索特定条目)

根据 django-syndication 中的示例,我在feeds.py中创建了这个简单的类

class SomeFeed(Feed):
    title = 'feed title '+request.feed_hash #just testing
    link = "/feed/"
    description = "Feed description"

    def items(self):
        return Item.objects.order_by('-published')[:5]

    def item_title(self, item):
        return item.title

    def item_description(self, item):
        return item.content

    # item_link is only needed if NewsItem has no get_absolute_url method.
    def item_link(self, item):
        return 'link'

因此我想知道,我如何修改它以根据哈希获得模型?

此时我无法以任何方式访问12e8e59187c328fbe5c48452babf769c。我如何访问它并 - 以标准的Django方式 - 从检索到的变量(代表访问多对多关系的slug)创建一个feed。)

1 个答案:

答案 0 :(得分:0)

首先,在django URL调度程序中设置参数。类似的东西:

url(r'^feed/(?P<pid>\w+)/$', SomeFeed())

现在使用Feed类中的get_object方法从URL检索并返回哈希值。毕竟,将哈希作为方法项()的第二个参数。

class SomeFeed(Feed):
    def get_object(self, request, pid):
        # expect pid as your second parameter on method items()
        return pid 

        # you can also load an instance here and get it the same way on items()
        return SomeFeed.objects.get(pk=pid)

    def items(self, feed):
        # filter your feed here based on the pid or whatever you need..
        return Item.objects.filter(feed=feed).order_by('-published')[:5]