👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
It is unclear to me how it is best to access URL-parameters in class-based-views in Django 1.5.
Consider the following:
View:
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+)/$",
view=Yearly.as_view(),
name="yearly-view"
),
)
I want to access the year
parameter in my view, so I can do logic like:
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
})
How would one best access the url parameter in CBVs like the above that is subclassed of TemplateView
and where should one ideally place the logic like this, eg. in a method?
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from URL-parameters and logic in Django class-based views (TemplateView), check other calendar Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano URL-parameters and logic in Django class-based views (TemplateView)
- Deutsch URL-parameters and logic in Django class-based views (TemplateView)
- Français URL-parameters and logic in Django class-based views (TemplateView)
- Español URL-parameters and logic in Django class-based views (TemplateView)
- Türk URL-parameters and logic in Django class-based views (TemplateView)
- Русский URL-parameters and logic in Django class-based views (TemplateView)
- Português URL-parameters and logic in Django class-based views (TemplateView)
- Polski URL-parameters and logic in Django class-based views (TemplateView)
- Nederlandse URL-parameters and logic in Django class-based views (TemplateView)
- 中文 URL-parameters and logic in Django class-based views (TemplateView)
- 한국어 URL-parameters and logic in Django class-based views (TemplateView)
- 日本語 URL-parameters and logic in Django class-based views (TemplateView)
- हिन्दी URL-parameters and logic in Django class-based views (TemplateView)
Paris | 2023-03-21
I was preparing for my coding interview, thanks for clarifying this - URL-parameters and logic in Django class-based views (TemplateView) in Python is not the simplest one. Will use it in my bachelor thesis
Paris | 2023-03-21
Simply put and clear. Thank you for sharing. URL-parameters and logic in Django class-based views (TemplateView) and other issues with Other Programming was always my weak point 😁. Will get back tomorrow with feedback
California | 2023-03-21
I was preparing for my coding interview, thanks for clarifying this - URL-parameters and logic in Django class-based views (TemplateView) in Python is not the simplest one. Checked yesterday, it works!