如何在 django 1.3 中创建 DetailView?

发布于 2024-11-07 12:13:21 字数 2562 浏览 0 评论 0原文

我目前正在学习如何使用 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 工作?

I'm currently learning how to use the class-based views in django 1.3. I'm trying to update an application to use them, but I still don't uderstand very well how they work (and I read the entire class-based views reference like two or three times EVERY day).

To the question, I have an space index page that needs some extra context data, the url parameter is a name (no pk, and that can't be changed, it's the expected behaviour) and the users that don't have that space selected in their profiles can't enter it.

My function-based code (working fine):

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))

My class-based view (not working, and no idea how to continue):

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()),
)

What am I missing for the DetailView to work?

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

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

发布评论

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

评论(1

梦初启 2024-11-14 12:13:21

我在您的代码中看到的唯一问题是您的网址的 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'

The only problem I see in your code is that your url's slug parameter is named 'space_name' instead of 'slug'. The view's slug_field attribute refers to the model field that will be used for slug lookup, not the url capture name. In the url, you must name the parameter 'slug' (or 'pk', when it's used instead).

Also, if you're defining a get_object method, you don't need the attributes queryset, model or slug_field, unless you use them in your get_object or somewhere else.

In the case above, you could either use your get_object as you wrote or define the following, only:

model = Space
slug_field = 'space_name'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文