简体   繁体   中英

Get request parameter in django syndication?

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.)

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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