带有Django-taggit的标签详细信息页面 [英] Tag detail page with Django-taggit

查看:160
本文介绍了带有Django-taggit的标签详细信息页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在django博客上为标签创建页面.我已经有一个简单的索引页面,其中显示了所有已使用标签的列表,现在我希望每个标签都有单独的页面,并且在该页面上,我将显示所有标记有该标签的帖子.这些标签详细信息页面的url结构如下:

Im trying to create pages for tags on my django blog. I already have a simple index page which displays a list of all used tags, now I want to have individual pages for each tag and on that page I will display all posts marked with that tag. The url structure for these tag detail pages will be like this

localhost/tag/my-tag-here

localhost/tag/my-tag-here

我已经安装了django-taggit并添加了一些标签,并且它们在上面提到的帖子详细信息页面和标签索引页面上显示得很好,但是当我尝试访问每个标签详细信息页面(例如/tag/)时,我收到了404错误测试.

I already have django-taggit installed and added some tags and I have them displaying fine on post detail pages and the tag index page mentioned above but Im getting a 404 when I try to visit each tag detail page such as /tag/test.

这些是我的文件以及下面的完整错误消息.

These are my files and the full error message below.

views.py

def tag_detail(request, tag):


    tag = get_object_or_404(Tag, tag=tag.name)


    return render(request, 'blog/tags_detail.html', {'tag': tag})

urls.py(应用程序)

urls.py (app)

urlpatterns = [

    url(r'^$', views.blog_index, name='blog_index'),
    url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/'\
        r'(?P<post>[-\w]+)/$',
        views.blog_post_detail,
        name='blog_post_detail'),
    url(r'^contact/$', views.contact_form, name='contact_form'),
    url(r'^thanks/$', views.thanks_view, name='thanks_view' ),
    url(r'^about/$', views.about, name='about'),
    url(r'^upload/$', views.upload_image, name='upload_image'),
    url(r'^tag/(?P<tag>[-/w]+)/$', views.tag_detail, name='tag_detail'),
    url(r'^tags/$', views.tags_index, name='tags_index')

]

这是完整的错误消息

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/tag/test

在我看来还是在URL结构中出现问题?对于该视图,我不确定100%是否能做到这一点的正确方法,但我尝试过与我的帖子详细视图相同的方法.

is the problem here in my view or the url structure? For the view I'm not 100% sure if thats the correct way to do it but Ive tried to do it the same as my post detail view.

谢谢

推荐答案

问题出在您的views.py文件中. 在此代码中:

The problem is in your views.py file. In this code:

def tag_detail(request, tag):

    tag = get_object_or_404(Tag, tag=tag.name)

    return render(request, 'blog/tags_detail.html', {'tag': tag})

您在这里写道:

 tag = get_object_or_404(Tag, tag=tag.name)

您在URL中传递了一个标记,因此正确的方法将是:

you passed a tag in URL so correct method would be :

 tag = get_object_or_404(Tag, tag=tag)

但这仅在您的模型中以Unicode返回标签名称的情况下才有效,例如:

But this will work only if, in your model,you have returned name of the tag as Unicode,Like this:

  class Tag(models.Model):
      name = models.CharField()

  def __unicode__(self):
      return unicode(self.name)

如果这仍然不起作用,则settings.py文件中的TEPLATE_DIR设置可能存在问题.然后,您必须共享用于项目文件结构的settings.py代码.

And if this still does not work then there might be a problem in TEPLATE_DIR setting in settings.py file. Then you have to share settings.py code for project file structure.

这篇关于带有Django-taggit的标签详细信息页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆