views:

597

answers:

4

Hi, I have the code in my urls.py for my generic views;

infodict = {
'queryset': Post.objects.all(),
'date_field': 'date',
'template_name': 'index.html',
'template_object_name': 'latest_post_list',
}

urlpatterns += patterns('django.views.generic.date_based',
(r'^gindex/$', 'archive_index', infodict),
)

So going to the address /gindex/ will use a generic view with the template of 'index.html'.

But since I will have more generic views in this urlpattern, how am I supposed to provide a different template name using the same infodict? I don't want to have to use lots of infodicts, and I can't use the default template name.

Please note this also applies to template object name within infodict.

Thanks for your help!

Edit: This is one of my first questions on stackoverflow and I am amazed with the thorough answers! I prefer using the dict constructor which I didn't know about. I find using the python documentation a bit harder as I can't find what i'm looking for usually!

Thanks again for all the answers and different approaches.

+1  A: 

If you want to supply different template names to different views, the common practice is indeed to pass in a unique dictionary to each URL pattern. For example:

urlpatterns = patterns('',
    url(r'^home/$', 'my.views.home', {'template_name': 'home.html'}, name='home'),
    url(r'^about/$', 'my.views.about', {'template_name': 'about.html'}, name='about'),
)

This kind of pattern is common and acceptable.

Ryan Duffield
-1 This doesn't address the question. If the infodict has five or six items, would you really duplicate all the common parameters across every url in separate dictionaries? I certainly wouldn't.
Carl Meyer
For readability, I might; especially if those arguments are likely to change in the future.
Ryan Duffield
Fair enough; the code works, retracting -1.
Carl Meyer
I still like your answer better. :-)
Ryan Duffield
A: 

Not as simple, but possibly useful if you have a whole lot of different patterns matching the same view:

base_dict={
...
#defaults go here
}
def make_dict(template_name,template_object_name):
    base_dict.update({
        'template_name':template_name,
        'template_object_name':template_object_name,
    })
    return base_dict

urlpatterns += patterns('django.views.generic.date_based',
(r'^gindex/$', 'archive_index', make_dict('index1.html','latest_poll_list')),
(r'^hindex/$', 'archive_index', make_dict('index2.html','oldest_poll_list')),
)

For a lot of similar generic views, this will condense your code calls a wee bit, at the expense of a little bit of transparency. If you've got many lines customizing the same few parameters, this might be easiest to read.

Finally, if all or most of your views require some, but not all, of the same information, never forget how useful a context processor is. It takes only a little more work to set up than the above solutions, but it extends much better, because it will guarantee that (unless you use the render_to_response shortcut without the RequestContext keyword) the defaults will always be available to your template no matter how your view or url conf changes.

David Berger
-1 The "update" code won't work; have you tried it? dict.update() doesn't return the updated dict, it updates the dict "in place" and returns None. And the other solution is unnecessary verbosity where you could just use dict(infodict, blah="blah").
Carl Meyer
You're right. However, I think the verbose example might be nicer if you had a lot of patterns to match. But probably not as useful.
David Berger
A: 

you can define wrapper view functions to parametrize generic views. In your urls.py add pattern

url(r'^/(?P<tmpl_name>\w+)/$', 'my.views.datebasedproxy')

in your views.py add view function

def datebasedproxy(request, tmpl_name):
    return django.views.generic.date_based(request,otherparameters,
    template_name=tmpl_matrix[tmpl_name])

where tmpl_matrix is hypothetic list that matches template file name with parameter and otherparameters stands for other dictionary items required for date_based function

kokeksibir
+4  A: 

Use the dict() constructor:

infodict = {
    'queryset': Post.objects.all(),
    'date_field': 'date',
    'template_name': 'index.html',
    'template_object_name': 'latest_post_list',
}

urlpatterns = patterns('django.views.generic.date_based',
    url(r'^gindex/$', 'archive_index', dict(infodict, template_name='gindex.html')),
    url(r'^hindex/$', 'archive_index', dict(infodict, template_name='hindex.html')),
)
Carl Meyer
+1 more in line with what was being asked.
Ryan Duffield