Commits

Slava Bacherikov  committed 2ff4f5a

Add search by tag

  • Participants
  • Parent commits 57144af

Comments (0)

Files changed (3)

File jabber_main/news/urls.py

 from news.views import NewsRssFeed
 
 urlpatterns = patterns('news.views',
-    url(r'^$', 'news_view', name = 'news'),
+    url(r'^(?:tag/(?P<tag>.*)/)?$', 'news_view', name = 'news'),
     url(r'^rss/$', NewsRssFeed(), name = 'news_rss'),
     url(r'^(?P<pk>\d+)/$', 'news_item_view', name = 'news_item'),
     url(r'^(?P<slug>[^/]+)/$', 'news_item_view', name = 'news_item'),

File jabber_main/news/views.py

     context_object_name = 'news'
     template_name = 'news.html'
     paginate_by = 10
-    queryset = News.objects.filter(published = True).order_by('-created_datetime')
+    queryset = News.objects.filter(published = True). \
+        order_by('-created_datetime')
+
+    def get_queryset(self):
+        q = super(NewsView, self).get_queryset()
+        tag = self.kwargs.get('tag')
+        if tag is not None:
+            q = q.filter(tags__name = tag)
+        return q
+        
 
 news_view = NewsView.as_view()
 

File jabber_main/templates/news.html

 {% extends 'base.html' %}
+{% load url from future %}
 {% block content %}
     {% for news_item in news %}
         <section class="list_news">
             {% if tags %}
             <div class="tags">
             <i class="icon-tags"></i>
-                {% for tag in tags %}<a href="">{{ tag }}</a>{% if not forloop.last %}, {% endif %}{% endfor %}
+                {% for tag in tags %}<a href="{% url 'news' tag=tag.name %}">{{ tag }}</a>{% if not forloop.last %}, {% endif %}{% endfor %}
             </div>
             {% endif %}
             {% endwith %}