【问题标题】:How to do a DetailView in django 1.3?如何在 django 1.3 中做一个 DetailView?
【发布时间】:2011-08-25 21:55:29
【问题描述】:

我目前正在学习如何在 django 1.3 中使用基于类的视图。我正在尝试更新应用程序以使用它们,但我仍然不太了解它们是如何工作的(而且我每天都会阅读两到三遍整个基于类的视图参考)。

对于这个问题,我有一个空间索引页面需要一些额外的上下文数据,url 参数是一个名称(没有 pk,并且无法更改,这是预期的行为)以及不需要的用户在他们的个人资料中选择了该空间无法进入。

我的基于函数的代码(工作正常):

def view_space_index(request, space_name):

    place = get_object_or_404(Space, url=space_name)

    extra_context = {
        'entities': Entity.objects.filter(space=place.id),
        'documents': Document.objects.filter(space=place.id),
        'proposals': Proposal.objects.filter(space=place.id).order_by('-pub_date'),
        'publication': Post.objects.filter(post_space=place.id).order_by('-post_pubdate'),
    }

    for i in request.user.profile.spaces.all():
        if i.url == space_name:
            return object_detail(request,
                                 queryset = Space.objects.all(),
                                 object_id = place.id,
                                 template_name = 'spaces/space_index.html',
                                 template_object_name = 'get_place',
                                 extra_context = extra_context,
                                )

    return render_to_response('not_allowed.html', {'get_place': place},
                              context_instance=RequestContext(request))

我的基于类的视图(不工作,不知道如何继续):

class ViewSpaceIndex(DetailView):

    # Gets all the objects in a model
    queryset = Space.objects.all()

    # Get the url parameter intead of matching the PK
    slug_field = 'space_name'

    # Defines the context name in the template
    context_object_name = 'get_place'

    # Template to render
    template_name = 'spaces/space_index.html'

    def get_object(self):
        return get_object_or_404(Space, url=slug_field)

    # Get extra context data
    def get_context_data(self, **kwargs):
        context = super(ViewSpaceIndex, self).get_context_data(**kwargs)
        place = self.get_object()
        context['entities'] = Entity.objects.filter(space=place.id)
        context['documents'] = Document.objects.filter(space=place.id)
        context['proposals'] = Proposal.objects.filter(space=place.id).order_by('-pub_date')
        context['publication'] = Post.objects.filter(post_space=place.id).order_by('-post_pubdate')
        return context

urls.py

from e_cidadania.apps.spaces.views import GoToSpace, ViewSpaceIndex
urlpatterns = patterns('',
    (r'^(?P<space_name>\w+)/', ViewSpaceIndex.as_view()),
)

要使 DetailView 正常工作,我缺少什么?

【问题讨论】:

    标签: django django-views django-class-based-views


    【解决方案1】:

    我在您的代码中看到的唯一问题是您的 url 的 slug 参数被命名为 'space_name' 而不是 'slug'。视图的 slug_field 属性是指将用于 slug 查找的模型字段,而不是 url 捕获名称。在 url 中,您必须将参数命名为 'slug'(或 'pk',当使用它时)。

    另外,如果您要定义get_object 方法,则不需要querysetmodelslug_field 属性,除非您在get_object 或其他地方使用它们。

    在上述情况下,您可以使用您所写的get_object,也可以仅定义以下内容:

    model = Space
    slug_field = 'space_name'
    

    【讨论】:

      猜你喜欢
      • 2019-05-05
      • 1970-01-01
      • 2015-06-08
      • 2012-03-13
      • 1970-01-01
      • 2012-04-09
      • 2019-08-29
      • 2012-01-04
      • 2018-07-06
      相关资源
      最近更新 更多