I have a view named events displaying two columns. One eventType and a second eventDates, listing dates. It looks like this:
eventType | eventDate |
---|---|
foo | 1900-01-01, 2010-02-02 |
bar | 2000-01-01, 2010-02-02, 2010-03-03 |
The code for the view and the table looks like:
def events(request, pk):
data = some_module.get_data(pk)
data_table = MyTable(data)
return render(request, "app/events.html", {"table": data_table})
Where data
is a list of dictionaries and MyTable
is a class inheriting from django_tables2.Table
:
class MyTable(tables.Table):
eventType = tables.Column()
eventDates = tables.Column()
A cell in the column eventDate can be either empty or containing several dates. It depends on pk
.
I can display that table. Now, I would like each date to be an hyperlink redirecting to another view. This view would be like that:
def event_details(request, eventtype=et, eventdate=ed):
data_event = some_module.get_event data(eventtype, eventdate)
return render(request, "app/events_details.html", {"data_event ": data_event })
And here are my issues. First, I have not been able to have a dynamic number of hyperlink, varying from cell to cell. Second, the url should include the eventType and its date. E.g. taking the exemple above, cliking on 2010-02-02 should redirect to either:
eventtype=foo_eventdate=2010-02-02 or eventtype=bar_eventdate=2010-02-02, depending on which 2020-02-02 I clik on.
I have first tried to have several links in one cell following some recomendations from e.g. django-tables2-linkcolumn-multiple-items-in-the-same-cell or using-linkify-option-on-django-tables2-columns-to-create-links but at the end of the day, my main issue are the ones stated above which I cannot find how to solve.
I don't have much information about the specific data and types used. However, if eventDate
can be provided as a list, the following method can be adapted to display a link for each individual date. This should provide a solid foundation for resolving your issue.
class MyTable(tables.Table):
event_type = tables.Column()
event_dates = tables.Column()
def render_event_dates(self, record, value):
dates = []
for date in value:
event_url = reverse(
"event_details",
kwargs={"event_type": record.event_type, "event_date": date},
)
dates.append(f'<a href="{event_url}">{date}</a>')
return mark_safe(", ".join(dates))