Reputation: 2764
I have this calendar app view that requires the current year and month, and is used across approximately 1/4 of the site so I was hoping to create a {% url %}
for it that wouldn't required updating the context everywhere.
I inherit a template with a JS script containing the following:
var url = "{% url 'companies:deliverers:deliverer-calendar-view' pk=company.pk slug=company.slugged_name year=now 'Y' month=now 'M' %}"
I was hoping to plug the year
and month
kwargs
by using the standard template tags, but this give be an error:
Don't mix *args and **kwargs in call to reverse()!
I also tried doing:
"{% url 'companies:deliverers:deliverer-calendar-view' pk=company.pk slug=company.slugged_name year=now'Y' month=now'M' %}"
But this gives me:
Could not parse the remainder: ''Y'' from 'now'Y''
Anyone have any suggestions?
Upvotes: 0
Views: 921
Reputation: 2764
Taking @Lucas Moeskops logic into account, I was able to develop a better solution:
{% now "Y" as YEAR %}
{% now "m" as MONTH %}
var url = "{% url 'companies:deliverers:deliverer-calendar-view' pk=company.pk slug=company.slugged_name year=YEAR month=MONTH %}"
Using this method you never have to hack the context_data
for a bunch of views... pretty nice :).
Upvotes: 1
Reputation: 5443
You need at least a date variable from the context. Then you can get away with:
{% with year=my_date|date:'Y' month=my_date|date:'M' %}
{% url 'companies:deliverers:deliverer-calendar-view' pk=company.pk slug=company.slugged_name year=year month=month %}
{% endwith %}
Upvotes: 1
Reputation: 195
I think you need to import datetime and do datetime.now()... You can pass that to a view through the context.
Upvotes: 0