2

Here is a url containing the hash for a super-secret feed:

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

I am trying to capture and send the variable '12e8e59187c328fbe5c48452babf769c' which is feed_hash (acts as a slug to retrieve the particular entry)

Based on the example in django-syndication, I've created this simple class in 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'

Hence I am wondering, how would I modify this to get a model according to the hash?

At this time I cannot access the 12e8e59187c328fbe5c48452babf769c in any way. How might I access this and -- in a standard Django way -- create a feed from the retrieved variable (which represents a slug accessing a many-to-many relationship.)

M.javid
  • 6,387
  • 3
  • 41
  • 56
1Up
  • 994
  • 2
  • 12
  • 24
  • I think the complex example from the Django feeds documentation is exactly what you want: https://docs.djangoproject.com/en/1.8/ref/contrib/syndication/#a-complex-example You capture the hash in urls.py and map it to a variable, which will be accessable in the feed function. – het.oosten Jul 04 '15 at 10:08

1 Answers1

1

First of all, set your parameter in django URL dispatcher. Something like:

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

Now retrieve and return the hash from the URL using the get_object method on your feed class. After all, get the hash as the second parameter of your method items().

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]
Fábio Gibson
  • 450
  • 4
  • 10