Hopefully, you will have been able to complete the exercises given the workflows we provided, if not, or if you need a little help checkout snippets of code and use them within your version of Rango.
16.1 Track Page Click Throughs
Currently, Rango provides a direct link to external pages. This is not very good if you want to track the number of times each page is clicked and viewed. To count the number of times a page is viewed via Rango you will need to perform the following steps.
Creating a URL Tracking View
Create a new view called track_url() in /rango/views.py which takes a parameterised HTTP GETrequest (i.e.rango/goto/?page_id=1) and updates the number of views for the page. The view
should then redirect to the actual URL. from django.shortcuts import redirect
def track_url(request): page_id = None
url = '/rango/'
if request.method == 'GET':
if 'page_id' in request.GET:
page_id = request.GET['page_id']
try:
page = Page.objects.get(id=page_id) page.views = page.views + 1 page.save() url = page.url except: pass return redirect(url)
from django.shortcuts import redirect
Mapping URL
In/rango/urls.pyadd the following code to theurlpatternstuple. url(r'^goto/$', views.track_url, name='goto'),
Updating the Category Template
Update thecategory.htmltemplate so that it usesrango/goto/?page_id=XXXinstead of providing
the direct URL for users to click.
{% for page in pages %} <li>
<a href="{% url 'goto' %}?page_id={{page.id}}">{{ page.title }}</a> {% if page.views > 1 %} ({{ page.views }} views) {% elif page.views == 1 %} ({{ page.views }} view) {% endif %} </li> {% endfor %}
Here you can see that in the template we have added some control statements to displayview,views
or nothing depending on the value of page.views.
Updating Category View
Since we are tracking the number of click throughs you can now update thecategory() view so
that you order the pages by the number of views, i.e:
pages = Page.objects.filter(category=category).order_by('-views')
Now, confirm it all works, by clicking on links, and then going back to the category page. Don’t forget to refresh or click to another category to see the updated page.
16.2 Searching Within a Category Page
Rango aims to provide users with a helpful directory of page links. At the moment, the search functionality is essentially independent of the categories. It would be nicer however to have search integrated into category browsing. Let’s assume that a user will first browse their category of interest first. If they can’t find the page that they want, they can then search for it. If they find a page that is suitable, then they can add it to the category that they are in. Let’s tackle the first part of this description here.
We first need to remove the global search functionality and only let users search within a category. This will mean that we essentially decommission the current search page and search view. After this, we’ll need to perform the following.
Decommissioning Generic Search
Remove the generic Search link from the menu bar by editing thebase.htmltemplate. You can also
remove or comment out the URL mapping inrango/urls.py.
Creating a Search Form Template
Take the search form fromsearch.htmland put it into thecategory.html. Be sure to change the
action to point to thecategory()view as shown below.
<form class="form-inline" id="user_form" method="post"
action="{% url 'category' category.slug %}"> {% csrf_token %}
<!-- Display the search form elements here --> <input class="form-control" type="text" size="50"
name="query" value="{{query}}" id="query" />
<input class="btn btn-primary" type="submit" name="submit" value="Search" /> <form\>
<div>
{% if result_list %} <h3>Results</h3>
<!-- Display search results in an ordered list --> <div class="list-group">
{% for result in result_list %} <div class="list-group-item">
<h4 class="list-group-item-heading">
<a href="{{ result.link }}">{{ result.title }}</a></h4> <p class="list-group-item-text">{{ result.summary }}</p> </div>
{% endfor %} </div>
{% endif %} </div>
Updating the Category View
Update the category view to handle a HTTPPOSTrequest (i.e. when the user submits a search) and
inject the results list into the context. The following code demonstrates this new functionality. def category(request, category_name_slug):
context_dict = {}
context_dict['result_list'] = None
context_dict['query'] = None if request.method == 'POST':
query = request.POST['query'].strip()
if query:
# Run our Bing function to get the results list! result_list = run_query(query)
context_dict['result_list'] = result_list context_dict['query'] = query
try:
category = Category.objects.get(slug=category_name_slug) context_dict['category_name'] = category.name
pages = Page.objects.filter(category=category).order_by('-views') context_dict['pages'] = pages
context_dict['category'] = category
except Category.DoesNotExist:
pass
if not context_dict['query']:
context_dict['query'] = category.name
Notice that in thecontext_dictthat we pass through, we will include theresult_listandquery,
and if there is no query, we provide a default query, i.e. the category name. The query box then displays this variable.