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