Step 6: Redirect View


The redirect view is easy. All it has to do is to look for the link in redis and redirect to it. Additionally we will also increment a counter so that we know how often a link was clicked:

def on_follow_short_link(self, request, short_id):
    link_target = self.redis.get('url-target:' + short_id)
    if link_target is None:
        raise NotFound()
    self.redis.incr('click-count:' + short_id)
    return redirect(link_target)

In this case we will raise a NotFound exception by hand if the URL does not exist, which will bubble up to the dispatch_request function and be converted into a default 404 response.