开发者

Django Date-Based Generic Views: How to Access Variables

开发者 https://www.devze.com 2023-03-10 23:24 出处:网络
I have a series of urls tied to Django\'s generic date views. In the extra_context parameter, I\'d like to pass in a queryset based off the year/ month variables in the URLs, but I\'m not sure how to

I have a series of urls tied to Django's generic date views. In the extra_context parameter, I'd like to pass in a queryset based off the year/ month variables in the URLs, but I'm not sure how to access them. For example, in

url(r'^archive/(?P<year>20[1-2][0-9])/?$', archive_year,
{'queryset': Article.objects.all(),
'date_field': 'publication_date',
'template_name': 'articles/archive-date-list.html',
'extra_context': {'content': 'articles'}},
name='article_archive'),

I'd like to add in the 5 most recent articles where the publication date's year is gte year and lt year + 1. Ideally, the collection would be looked up on each request and not just cached at compile time. Am I better 开发者_JS百科off writing a context processor for this/ extending the view?


You create a wrapper around the generic view:

# myapp/views.py

def my_archive_year(request, year):
    # Logic to get the articles here

    return archive_year(request,
        year=year,
        date_field='publication_date',
        template_name='articles/archive-date-list.html',
        extra_context = {'content': articles}
    )

# urls.py

url(r'^archive/(?P<year>20[1-2][0-9])/?$', 'myapp.views.my_archive_year', name='article_archive'),
0

精彩评论

暂无评论...
验证码 换一张
取 消