Reputation: 1150
I am attempting to pass a URL parameter to another view
within my app. I currently have a function (EntryListView
) that identifies what month you select, and then only shows the contents for that month. However, I would like the month to also be shown in the detail
URL. This will enable a "go-back" button to navigate back to the month's page, as opposed to erroring out or navigating back to the landing page.
urls.py As Is:
url(r'entry/list/(?P<month>\w+)$', views.EntryListView.as_view(), name='entry-list'),
url(r'entry/detail/(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
views.py
class DetailView(generic.DetailView):
model = Entry
template_name = 'argent/detail.html'
class EntryListView(generic.ListView):
template_name = 'argent/index_list.html'
queryset = Entry.objects.all()
def get_context_data(self, **kwargs):
month = self.kwargs.get('month')
ctx = super(EntryListView, self).get_context_data(**kwargs)
# January
if month == 'January':
ctx['January17_qs'] = Entry.objects.filter(date__range=('2017-1-1', '2017-1-31'))
# February17
elif month == 'February':
ctx['February17_qs'] = Entry.objects.filter(date__range=('2017-2-1', '2017-2-28'))
# March
elif month == 'March':
ctx['March17_qs'] = Entry.objects.filter(date__range=('2017-3-1', '2017-3-31'))
return ctx
template
<a href="{% url 'argent:entry-list' %}">
<button type="button" class="btn btn-primary">Go Back
</button>
</a>
I currently get this error when using {% url 'argent:entry-list' %}
:
Reverse for 'entry-list' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['tracker/entry/list/(?P<month>\\w+)$']
Thanks in advance for your help!
Upvotes: 1
Views: 1871
Reputation: 8250
You can pass the details's month to the url
template tag as kwarg. Something like this should work.
class DetailView(generic.DetailView):
model = Entry
template_name = 'argent/detail.html'
def get_context_data(self, **kwargs):
ctx = super(DetailView, self).get_context_data(**kwargs)
ctx['current_month'] = self.get_object().date.strftime("%B")
return ctx
template.html
<a href="{% url 'argent:entry-list' current_month %}">
<button type="button" class="btn btn-primary">Go Back</button>
</a>
Upvotes: 3