Django评论框架中的get_comment_permalink如何工作?

时间:2012-03-27 08:11:48

标签: python django django-comments

我不太了解Django评论框架中的get_comment_permalink

我使用Django的评论为我的班级Order创建了一些评论,默认情况下,它会显示类似/comments/cr/18/1/#c1的内容,并且该网址永远不存在。

我看了评论的urls.py,它有一行说

urlpatterns += patterns('',
    url(r'^cr/(\d+)/(.+)/$', 'django.contrib.contenttypes.views.shortcut', name='comments-url-redirect'),
)

具有views.py方法的shortcut

from django import http
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site, get_current_site
from django.core.exceptions import ObjectDoesNotExist
from django.utils.translation import ugettext as _

def shortcut(request, content_type_id, object_id):
    """
    Redirect to an object's page based on a content-type ID and an object ID.
    """
    # Look up the object, making sure it's got a get_absolute_url() function.
    try:
        content_type = ContentType.objects.get(pk=content_type_id)
        if not content_type.model_class():
            raise http.Http404(_(u"Content type %(ct_id)s object has no associated model") %
                               {'ct_id': content_type_id})
        obj = content_type.get_object_for_this_type(pk=object_id)
    except (ObjectDoesNotExist, ValueError):
        raise http.Http404(_(u"Content type %(ct_id)s object %(obj_id)s doesn't exist") %
                           {'ct_id': content_type_id, 'obj_id': object_id})

    try:
        get_absolute_url = obj.get_absolute_url
    except AttributeError:
        raise http.Http404(_("%(ct_name)s objects don't have a get_absolute_url() method") %
                           {'ct_name': content_type.name})
    absurl = get_absolute_url()

    # Try to figure out the object's domain, so we can do a cross-site redirect
    # if necessary.

    # If the object actually defines a domain, we're done.
    if absurl.startswith('http://') or absurl.startswith('https://'):
        return http.HttpResponseRedirect(absurl)

    # Otherwise, we need to introspect the object's relationships for a
    # relation to the Site object
    object_domain = None

    if Site._meta.installed:
        opts = obj._meta

        # First, look for an many-to-many relationship to Site.
        for field in opts.many_to_many:
            if field.rel.to is Site:
                try:
                    # Caveat: In the case of multiple related Sites, this just
                    # selects the *first* one, which is arbitrary.
                    object_domain = getattr(obj, field.name).all()[0].domain
                except IndexError:
                    pass
                if object_domain is not None:
                    break

        # Next, look for a many-to-one relationship to Site.
        if object_domain is None:
            for field in obj._meta.fields:
                if field.rel and field.rel.to is Site:
                    try:
                        object_domain = getattr(obj, field.name).domain
                    except Site.DoesNotExist:
                        pass
                    if object_domain is not None:
                        break

    # Fall back to the current site (if possible).
    if object_domain is None:
        try:
            object_domain = get_current_site(request).domain
        except Site.DoesNotExist:
            pass

    # If all that malarkey found an object domain, use it. Otherwise, fall back
    # to whatever get_absolute_url() returned.
    if object_domain is not None:
        protocol = request.is_secure() and 'https' or 'http'
        return http.HttpResponseRedirect('%s://%s%s'
                                         % (protocol, object_domain, absurl))
    else:
        return http.HttpResponseRedirect(absurl)

这对我来说太复杂了。

当Django说固定链接时,我会考虑在页面上引用特定的地方(通常是标题)。例如,Django的评论框架文档是链接#1,您可以使用链接#2永久链接“链接到评论”部分。

1. https://docs.djangoproject.com/en/dev/ref/contrib/comments/
2. https://docs.djangoproject.com/en/dev/ref/contrib/comments/#linking-to-comments

所以对于评论,不应该是一样的吗?网址不应该只是#c1或没有/comments/cr/18/1/...的内容吗?事实上,我甚至不知道Django在哪里181 ...从shortcut方法,我知道18content_type_id1object_id,但如何判断models.py中的哪个类是哪个内容类型ID和对象ID?

2 个答案:

答案 0 :(得分:4)

评论框架使用Generic RelationsComment个对象链接到您的数据库对象(在您的案例中为Order模型)。通用关系允许一个对象与另一个对象保持关系,而无需明确知道它的类。您可以在此处查看创建通用关系的字段(content_type,object_pk,content_object)以供评论:django.contrib.comments.models

一旦评论完成并附加到特定类的实例(例如单个Order),我们需要一种方法来获取该特定评论的链接(永久链接)。要获取评论的链接,我们需要知道评论所针对的对象的URL(同样,您的案例中的特定Order)。这就是get_comment_permalink正在做的事情 - 它构建了一个已经留下注释的对象的URL,并且还将一个锚链接(#c1部分)附加到URL,以便浏览器跳转到对该页面的特别评论。

要做到这一切,它有3个步骤:

  • 首先通过查找通用关系找出它正在处理的对象类型。这将为我们留下Order对象
  • 现在它尝试获取该对象的绝对URL get_absolute_url。这可能是/ order / my-order /
  • 使用Sites framework
  • 构建URL的“http://mysite.com/”部分
  • 它找出了网址
  • 的#c31(评论的锚点链接)部分

现在我们有一个完整的http://mysite.com/order/my-order/c#31,它会将我们带到正确的页面并显示正确的评论

答案 1 :(得分:2)

  

所以对于评论,不应该是一样的吗?网址不应该是#c1或没有/ comments / cr / 18/1 / ...的东西吗?事实上我甚至不知道Django在哪里得到18和1 ......从快捷方法来看,我明白18是content_type_id而1是

18是内容类型id,1是对象id。快捷方式视图使用这些参数从数据库中提取对象,并重定向到modelobject.get_absolute_url()

在模型中定义/修复get_absolute_url()方法,这将修复django.contrib.contenttypes.views.shortcut

也就是说,Django期望模型对象的url显示该对象的注释列表。在这种情况下,只需在单个评论HTML中添加<a name="c{{ comment.id }}"></a>