Django Tagging - 分离标签的问题

发布于 2024-11-17 02:00:55 字数 1024 浏览 3 评论 0原文

我在 django 标记方面遇到了麻烦 - 按由多个单词组成的标记进行过滤不起作用。 模型

class MyWidget(models.Model):
    """Widget for tagging. It's damn bugged, so it need try except statement
    for be able to import in shell and work in general. Otherwise it's raiseing
    exception."""
    name = models.CharField(max_length = 50)
try:
    tagging.register(MyWidget)
except tagging.AlreadyRegistered:
    pass

视图

class PostsByTags(ListView):
    """View returns news filtered by tag."""
    template_name = 'news.djhtml'
    contect_object_name = 'news_list'
    def get_queryset(self):
        tags = unquote(self.kwargs['tag'])
        return TaggedItem.objects.get_by_model(News(), tags)

url

url(r'^news/(?P<tag>[\w\s]*)/$',      views.PostsByTags.as_view(),
                                            name = 'tagged_news'),       

如果 url 看起来像这样

/news/separated%20tag/

视图类返回空列表。到底是什么原因呢?我该如何修复它?

I have got trouble with django tagging - filtering by tags consisting of several words does not work.
models

class MyWidget(models.Model):
    """Widget for tagging. It's damn bugged, so it need try except statement
    for be able to import in shell and work in general. Otherwise it's raiseing
    exception."""
    name = models.CharField(max_length = 50)
try:
    tagging.register(MyWidget)
except tagging.AlreadyRegistered:
    pass

views

class PostsByTags(ListView):
    """View returns news filtered by tag."""
    template_name = 'news.djhtml'
    contect_object_name = 'news_list'
    def get_queryset(self):
        tags = unquote(self.kwargs['tag'])
        return TaggedItem.objects.get_by_model(News(), tags)

urls

url(r'^news/(?P<tag>[\w\s]*)/

If url looking something like that

/news/separated%20tag/

the view class returns empty list. What the cause? How can I fix it?

, views.PostsByTags.as_view(), name = 'tagged_news'),

If url looking something like that

/news/separated%20tag/

the view class returns empty list. What the cause? How can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的心境我的脸 2024-11-24 02:00:55

我认为查询标签被分成两部分(在 tagging.utils.parse_tag_input 中)。

get_by_model 也可以采用标签项而不是字符串,因此请尝试类似的操作。

tag_object = Tag.objects.get(name=self.kwargs['tag'])
TaggedItem.objects.get_by_model(News(), tag_object)

I thinks the query tag is being split into two (in tagging.utils.parse_tag_input).

get_by_model can also take a Tag item rather then a string, so try something like.

tag_object = Tag.objects.get(name=self.kwargs['tag'])
TaggedItem.objects.get_by_model(News(), tag_object)
相对绾红妆 2024-11-24 02:00:55

格式化你的网址怎么样:

url(r'^news/((?P<tag>[^/]+))/
,      views.PostsByTags.as_view(),
                                            name = 'tagged_news'),       

What about formatting your url:

url(r'^news/((?P<tag>[^/]+))/
,      views.PostsByTags.as_view(),
                                            name = 'tagged_news'),       
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文