Как реализовать поиск по тегам используя django-taggit?

Кастую гуру джанги, что я делаю не так? Получаю 404
models.py :
from taggit.managers import TaggableManager
class Article(models.Model):
    title = models.CharField(max_length=50)
    pub_date = models.DateTimeField('date published')
    text = models.TextField()
    tags = TaggableManager()


views.py:
def index(request):
    lastet_articles_list = Article.objects.order_by('-pub_date')
    all_tags = []
    for article in lastet_articles_list:
        all_tags += article.tags.names()
    all_tags = list(set(all_tags))
    context = {
        'lastet_articles_list': lastet_articles_list,
        'all_tags': all_tags,
    }
    return render(request, 'article/index.html', context)


def article(request, article_id):
    article = get_object_or_404(Article, id=article_id)
    return render(request, 'article/article.html', {'article': article})

def search_by_tag(request, tag):
    articles_with_tag = Article.objects.filter(tags__name__in=[tag])
    return render(request, 'article/tags.html', {'articles_with_tag': articles_with_tag})


urls.py:
...
url(r'^tag/(<tag>[a-zA-Z])+/$', views.search_by_tag, name='search'),


index.html:
...
            {% if all_tags %}
                {% for tag in all_tags %}
                    <li><a href="tags/{{ tag }}">{{ tag }}</a></li>
                {% endfor %}
            {% endif %}
...


tags.html:
{% if articles_with_tag %}
            {% for article in articles_with_tag %}
                <div class="post-preview">
                    <a href="/article/{{ article.id }}/">{{ article.title }}</a><br>
                    {% for article_tag in article.tags.names %}
                        <span class="tags"><i class="fa fa-tag fa-lg"></i>{{ article_tag }}</span>
                    {% endfor %}
                    <p class="post-meta">Posted {{ article.pub_date }}</p>
                </div>
                <hr>
            {% endfor %}
        {% endif %}
  • Вопрос задан
  • 1106 просмотров
Решения вопроса 1
winordie
@winordie
Лучшая документация -- исходники
url(r'^tag/(?P<tag>[a-zA-Z]+)/$', views.search_by_tag, name='search'),
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы