Reputation: 95
I have a Django website that basically just displays a bunch of items from a database with searching, filtering, etc. The site is publicly accessible, with no login or anything. What I want to do is the first time a user visits the site, prompt them for a filter choice, then save that filter choice (on their machine would be preferable, would a browser cookie work here?) so that every future time they visit the website, it's displayed with that filter.
So I have two specific questions here:
1) What's the best way to store their choice?
2) What's the best way to collect their choice? The way I'm envisioning this behaving is the first time they visit the site, they're presented with a "pop-up" of sorts, that basically asks for their filter choice, the continues on to the main site page with their filter choice enabled. The problem I'm having is that the filter choice that they pick changes the content that would be displayed on the website. Is there a way to avoid reloading the entire page once I have their filter choice?
Here's a decent example of something like the popup I'm trying to create: http://www.wizards.com/wpn/events/rules.aspx
Thanks, -Alexei
Upvotes: 0
Views: 756
Reputation: 1450
Giving a cookie to the user would definitely work. In web development the technique is known as creating a 'session' for the user so that every time they request a page from your site, the request can be identified as part of that particular session and serviced accordingly.
Django ships with an excellent and very easy to use sessions framework. You should read the documentation here, but I will summarise the steps you'll need to take.
django.contrib.sessions.middleware.SessionMiddleware
is in your MIDDLEWARE_CLASSES
variable in the settings file.SESSION_COOKIE_AGE
that meets your requirements. You can set this number very high if you want users' choices to last for a long time.Once you've done this, the request
variable that is passed into your views will have a session
attribute. session
is an object which you can use like a dictionary to store information. The magic bit is that that information will be unique to every user who makes requests to your site.
This partial code demonstrates how to return content based on a previously selected user choice stored in their session:
def get_front_page_contents(request):
filter = request.session.get('filter_choice', False)
if filter:
# do the filtering
render_to_response(template_name,
{'filtered_content': content},
RequestContext(request))
else:
# the user has not made a choice so
# return a default result set or redirect
# to form that allows them to make a choice
Your second question was how to collect this choice in the first place. Here's a simple solution that uses multiple page loads.
request.session
has a choice saved for this user.
request.session
for this user. Return their desired content. After this process, when the same user comes back to step one at a later date, their desired content would be displayed.
Avoiding page reloads is a separate issue, but it's easily done with javascript and AJAX techniques. You'll probably want to serve a minimal page which includes javascript to make the requests for content. These AJAX requests can be examined in exactly the same way to check for the presence of session data.
Upvotes: 1