Reputation: 210
Here is my problem :
I have a list of messages which I can filter using a form on the same page. Also, I can display one of the message below the list by clicking on it.
My problem is that if I filter the list and then selects a message to display it, the GET data filtering the list are 'lost'. So all the messages are displayed in the list again.
How could I display a message and keep the list as it was when I clicked on the message?
My urls:
url(r'^inbox/$', view='inbox', name="kernel-networking-messages-inbox"),
url(r'^inbox/(?P<message_pk>\d+)/$', view='inbox_message', name="kernel-networking-messages-inbox-read"),
My views:
class InboxView(SearchViewMixin):
template_name = "kernel/networking/messages/inbox.html"
form_class = InboxForm
inbox = InboxView.as_view()
class InboxMessageView(InboxView):
def dispatch(self, request, *args, **kwargs):
self.message = get_object_or_404(Message, pk=kwargs['message_pk'])
return super(InboxMessageView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(InboxMessageView, self).get_context_data(**kwargs)
context['message'] = self.message
return context
inbox_message = InboxMessageView.as_view()
inbox.html:
<form action="" method="get">
...
<input type="submit" value="Search"/>
<table>
...
</table>
{% if message %}
// display message
{% endif %}
I tried to be as clear as possible :/
Upvotes: 0
Views: 317
Reputation: 1376
I see two options.
1: Use Django's session framework.
When you receive a request for filtering, store the filter options in the session: request.session['filter_options'] = filter_option_dict
Then, when you load the page and don't see any filtering options in request.GET, check if there are any options in the session:
if(len(request.GET) > 0):
#get filter options from request.GET
#save filter options to session
elif('filter_options' in request.session):
#get filter options from session
else:
#no filter options, display without filtering
Note that the session is designed for temporary data, and is specific to one browser on one computer. That's not a problem for this use case, but it's something to remember.
2: Use ajax to load the message without changing the rest of the page. On the server, you create a view that returns a message without the page header or any other formatting, and javascript on the front-end to send requests and insert the retrieved messages into the page. This is very easy with JQuery, although it can be done without.
This would be my preferred solution, as websites that update themselves interactively feel much nicer from a user's perspective than one that has to refresh every time they click a link.
Upvotes: 1