django: passing extra argument to the template with generic view class

回眸只為那壹抹淺笑 提交于 2019-12-22 12:44:09

问题


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.


回答1:


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.




回答2:


I've been doing it by subclassing the generic view, like this:

In urls.py:

url(r'^processador/(?P<pk>[\w-]+)/$', ProcessadorUpdateView.as_view(), name='processador-detail'),
url(r'^software/(?P<pk>[\w-]+)/$', SoftwareUpdateView.as_view(), name='software-detail'),

And in views.py:

class ProcessadorUpdateView(UpdateView):
    model=Processador
    template_name='model_form.html'
    success_url=reverse_lazy('processador-list') # I'm not sure this will work; I've used get_success_url method
    def get_context_data(self, **context):
        context[self.context_object_name] = self.object
        context["extrainfo"] = "Processador"
        return context

In fact, I always create my own subclasses, even if there is no need for any extra functionality; this way I have more control and the views and urlconfigs are clearly separated.



来源:https://stackoverflow.com/questions/11588743/django-passing-extra-argument-to-the-template-with-generic-view-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!