开发者

Storing visitor choice in Django

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 开发者_Go百科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


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.

  1. Enable the Django sessions middleware (this is already done in the default Django project). Make sure django.contrib.sessions.middleware.SessionMiddleware is in your MIDDLEWARE_CLASSES variable in the settings file.
  2. Choose a sessions backend. Database sessions will work well enough to begin with (if performance becomes a problem later, consider cached sessions with Memcached providing storage).
  3. Set an appropriate 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.

  1. On initial request to the home page, check whether the request.session has a choice saved for this user.
    • If they do, render a page containing filtered content (and you're done).
    • If not, redirect to a form where they can make a choice.
  2. On form submission, store the choice in 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜