English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Django يحتوي على إطار عمل لإنشاء تغذية الـ feed. بفضل ذلك، يمكنك إنشاء RSS أو Atom فقط عن طريق توريث دالة django.contrib.syndication.views.Feed.
دعونا نخلق تطبيقًا لإنشاء مصدر الاشتراك.
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : ar.oldtoolbag.com # Date : 2020-08-08 from django.contrib.syndication.views import Feed from django.contrib.comments import Comment from django.core.urlresolvers import reverse class DreamrealCommentsFeed(Feed): title = "تعليقات Dreamreal's" link = "/drcomments/" description = "تحديثات على التعليقات الجديدة في مدونة Dreamreal." def items(self): return Comment.objects.all().order_by("-submit_date")[:5] def item_title(self, item): return item.user_name def item_description(self, item): return item.comment def item_link(self, item): return reverse('comment', kwargs = {'object_pk':item.pk})
في فئة feed، خصائص title، link و description تتطابق مع عناصر RSS القياسية <title>، <link> و <description>.
يستعيد طريقة البند العناصر التي يجب أن تدخل في feed. في مثالنا هي خمس تعليقات أحدث.
الآن، لدينا feed، ونضيف التعليقات في عرض views.py لعرض تعليقاتنا -
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : ar.oldtoolbag.com # Date : 2020-08-08 from django.contrib.comments import Comment def comment(request, object_pk): mycomment = Comment.objects.get(object_pk = object_pk) text = '<strong>User :</strong> %s <p>'%mycomment.user_name</p> text += '<strong>Comment :</strong> %s <p>'%mycomment.comment</p> return HttpResponse(text)
نحن بحاجة إلى بعض الروابط التي يتم توجيهها في ملف myapp urls.py −
# Filename : example.py # Copyright : 2020 By w3codebox # Author by : ar.oldtoolbag.com # Date : 2020-08-08 from myapp.feeds import DreamrealCommentsFeed from django.conf.urls import patterns, url urlpatterns += patterns('', url(r'^latest/comments/', DreamrealCommentsFeed()), url(r'^comment/(?P\w+)/', 'comment', name = 'comment'), )
عند زيارة /myapp/latest/comments/ سيتم الحصول على feed −
عند النقر على اسم مستخدم معين، سيتم الحصول على: /myapp/comment/comment_id قبل تعريف عرض التعليقات الخاصة بك، سيتم الحصول على −
لذلك، يجب تعريف مصدر RSS كنوع فرعي من فئة Feed، وتأكد من تعريف هذه الURL (أحدى الURLات مخصصة للوصول إلى feed، والأخرى مخصصة للوصول إلى عناصر feed).