URL-параметры и логика в представлениях на основе классов Django (TemplateView)

Мне неясно, как лучше всего получить доступ к URL-параметрам в представлениях на основе классов в Django 1.5.

Учтите следующее:

Посмотреть:

from django.views.generic.base import TemplateView


class Yearly(TemplateView):
    template_name = "calendars/yearly.html"

    current_year = datetime.datetime.now().year
    current_month = datetime.datetime.now().month

    def get_context_data(self, **kwargs):
        context = super(Yearly, self).get_context_data(**kwargs)
        context['current_year'] = self.current_year
        context['current_month'] = self.current_month
        return context

привязок:

from .views import Yearly


urlpatterns = patterns('',
    url(
        regex=r'^(?P\d+)/

Я хочу получить доступ кyear параметр на мой взгляд, поэтому я могу сделать логику, как:

month_names = ["January", "February", "March", "April", "May", "June", "July",
               "August", "September", "October", "November", "December"]

     for month, month_name in enumerate(month_names, start=1):
        is_current = False
        if year == current_year and month == current_month:
            is_current = True
        months.append({
            'month': month,
            'name': month_name,
            'is_current': is_current})

Как лучше всего получить доступ к параметру url в CBV, как указано выше, который подклассTemplateView и где нужно в идеале поместить логику, например, так: в методе?

, view=Yearly.as_view(), name='yearly-view' ), )

Я хочу получить доступ кyear параметр на мой взгляд, поэтому я могу сделать логику, как:

month_names = ["January", "February", "March", "April", "May", "June", "July",
               "August", "September", "October", "November", "December"]

     for month, month_name in enumerate(month_names, start=1):
        is_current = False
        if year == current_year and month == current_month:
            is_current = True
        months.append({
            'month': month,
            'name': month_name,
            'is_current': is_current})

Как лучше всего получить доступ к параметру url в CBV, как указано выше, который подклассTemplateView и где нужно в идеале поместить логику, например, так: в методе?

Ответы на вопрос(5)

Ваш ответ на вопрос