在django 1.3中如何做DetailView? [英] How to do a DetailView in django 1.3?

查看:198
本文介绍了在django 1.3中如何做DetailView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在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),
'proposal':Proposal.objects.filter(space = place.id).order_by(' - pub_date'),
'publication':Post.objects.filter(post_space = pla ce.id).order_by(' - post_pubdate'),
}

在request.user.profile.spaces.all()中:
如果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,


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

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

  class ViewSpaceIndex(DetailView):

#获取所有对象模型
queryset = Space.objec ts.all()

#获取匹配PK
的url参数整数slug_field ='space_name'

#定义模板中的上下文名称
context_object_name ='get_place'

#模板渲染
template_name ='spaces / space_index.html'

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

#获取额外的上下文数据
def get_context_data(self,** kwargs):
context = super(ViewSpaceIndex,self).get_context_data (** kwargs)
place = self.get_object()
上下文['entities'] = Entity.objects.filter(space = place.id)
上下文['documents'] = Document.objects.filter(space = place.id)
上下文['proposal'] = Proposal.objects.filter(space = place.id).order_by(' - pub_date')
context [' publication'] = Post.objects.filter(post_space = place.id).order_by(' - post_pubdate')
返回上下文

urls.py



<$ p $来自e_cidadania.apps.spaces.views的p> 导入GoToSpace,ViewSpaceIndex
urlpatterns = patterns('',
(r'^(?P< space_name> \w +)/ ',ViewSpaceIndex.as_view()),

工作?

解决方案

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



另外,如果要定义 get_object 方法,则不需要属性 queryset 模型 slug_field ,除非您在 get_object 或其他地方。



在上述情况下,您可以使用您的 get_object


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?

解决方案

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'

这篇关于在django 1.3中如何做DetailView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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