django:使用通用视图类将额外的参数传递给模板 [英] django: passing extra argument to the template with generic view class

查看:684
本文介绍了django:使用通用视图类将额外的参数传递给模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用相同的模板来显示来自django中的不同模型的记录,使用泛型类查看器。通用类查看器已经接受了模板中所需的大部分参数,除了一个。



如何在上下文中将这个额外的参数传递给模板? p>

我尝试将其作为urlconf中的第三个(额外)参数传递,没有成功:

 urlconf.py 
url(r'^ processador /(?P< pk> [\w - ] +)/ $',
UpdateView.as_view(
model = Processador,
template_name ='model_form.html',
success_url = reverse_lazy('processador-list'),
),
{'extrainfo':Processador },
name ='processador-detail'
),

url(r'^ software /(?P< pk> [\w - ] +)/ $',
UpdateView.as_view(
model = Software,
template_name ='model_form.html',
success_url = reverse_lazy('software-list'),

{'extrainfo':Software},
name ='software-detail'

在我的应用程序中会有几个这样的urlconfs。



一种可能性是对class类进行子类化,并提供我自己的get_context_data方法的实现,该方法添加了所需的键值对。



但是这个解决方案太重复了,因为它将被应用于view类的每一次使用。



也许只能使view类的一个子类。这个新类中的as_view类方法将接受一个新的命名参数,它将在get_context_data的重新定义中进入上下文。



我在django和Python中没有经验,所以我不知道如何完成这个,我接受帮助。

解决方案

我想你可以这样做只有一个 UpdateView 的子类,而不是我认为您认为您需要的每个模型。



参数 as_view 将所设置的属性设置为返回的对象,因此我认为您可以执行

  class MyUpdateView(UpdateView):
extrainfo = None

def get_context_data(self,** kwargs):
context = super(MyUpdateView,self).get_context_data ,** kwargs)
上下文['extrainfo'] = self.extrainfo

返回上下文

然后在你的urlconf中调用它,如

  url(r'^ processador /(?P< pk> [\w  - ] +)/ $',
MyUpdateView.as_view(
model = Processador,
template_name ='model_form.html',
success_url = reverse_lazy('processador-list'),
extrainfo =Processador
),
name = 'processador-detail'

我不确定你应该这样做尽管如此,它的标题是 urls.py 中的太多东西。


I want to use the same template to display records from different models in django, with generic class viewers. The generic class viewers already accept most of the arguments needed in the template, except for one.

How can I pass this extra argument in the context to the template?

I have tried passing it as the third (extra) argument in the urlconf, without success:

# in urlconf.py
url(r'^processador/(?P<pk>[\w-]+)/$',
    UpdateView.as_view(
        model=Processador,
        template_name='model_form.html',
        success_url=reverse_lazy('processador-list'),
        ),
    {'extrainfo': "Processador"},
    name='processador-detail'
),

url(r'^software/(?P<pk>[\w-]+)/$',
    UpdateView.as_view(
        model=Software,
        template_name='model_form.html',
        success_url=reverse_lazy('software-list'),
        ),
    {'extrainfo': "Software"},
    name='software-detail'
),

There will be several urlconfs like these in my application.

One possibility is sub-classing the view class and provide my own implementation of the get_context_data method which add the desired key-value pair.

But this solution is too repetitive, as it would be applied to every use of the view class.

Maybe it is possible to make only one subclass of the view class. The as_view class method in this new class would accept a new named argument which would go into the context in the redefinition of get_context_data.

I am not too experienced in django and Python, so I am not sure how to accomplish this and I am accepting help.

解决方案

I think you can do this with only one subclass of UpdateView, rather than the one per model that I think you think you need.

Arguments to as_view get set as attributes on the object which is returned, so I think you could do

class MyUpdateView(UpdateView):
    extrainfo = None

    def get_context_data(self, **kwargs):
        context = super(MyUpdateView, self).get_context_data(self, **kwargs)
        context['extrainfo'] = self.extrainfo

        return context

And then call this in your urlconf like

url(r'^processador/(?P<pk>[\w-]+)/$',
    MyUpdateView.as_view(
        model=Processador,
        template_name='model_form.html',
        success_url=reverse_lazy('processador-list'),
        extrainfo="Processador"
        ),
    name='processador-detail'
)

I'm not at all sure you should do this though - it's heading towards too much stuff in the urls.py.

这篇关于django:使用通用视图类将额外的参数传递给模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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