Parametry URL i logika w widokach opartych na klasach Django (TemplateView)
Nie jest dla mnie jasne, jak najlepiej uzyskać dostęp do parametrów URL w widokach klas w Django 1.5.
Rozważ następujące:
Widok:
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
URLCONF:
from .views import Yearly
urlpatterns = patterns('',
url(
regex=r'^(?P<year>\d+)/Chcę uzyskać dostęp doyear
moim zdaniem parametr, więc mogę zrobić logikę taką jak:
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})
Jak najlepiej uzyskać dostęp do parametru url w CBV takich jak powyżej, który jest podklasowanyTemplateView
i gdzie najlepiej umieścić taką logikę, np. w metodzie?
,
view=Yearly.as_view(),
name='yearly-view'
),
)
Chcę uzyskać dostęp doyear
moim zdaniem parametr, więc mogę zrobić logikę taką jak:
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})
Jak najlepiej uzyskać dostęp do parametru url w CBV takich jak powyżej, który jest podklasowanyTemplateView
i gdzie najlepiej umieścić taką logikę, np. w metodzie?