title: Dividing responsibility between View and Template in Django
body: <p>There's a feature in Twitter called hashtags that extracts tags from messages.<br>
  For example, in</p>
  
  <pre><code>"Hey #guys, I love #stackoverflow"
  </code></pre>
  
  <p><code>#guys</code> and <code>#stackoverflow</code> are clickable and lead to their own queries.</p>
  
  <p>I'm building a similar app for training, and I wonder <strong>where and when to actually inject links in the output</strong>?</p>
  
  <p>I grab all recent tweets:</p>
  
  <pre><code>def index(request):
      latest_tweets = Tweet.objects.all().order_by('-pub_date')[:5]
      return render_to_response('index.html',
                                { 'tweets': latest_tweets },
                                context_instance=RequestContext(request)
                                )
  </code></pre>
  
  <p>And in a block I render:</p>
  
  <pre><code>{% for tweet in tweets %}
      &lt;p&gt;{{tweet.message}}&lt;/p&gt;
  {% endfor %}
  </code></pre>
  
  <p>But this of course only shows plain message, no links.<br>
  I want each <code>#tag</code> in <code>message</code> to be replaced with a link passing tag name in a query string to a view called <code>by_tag</code>.</p>
  
  <p><strong>Does inserting <code>&lt;a href="..."&gt;</code>s belong to <code>views.py</code>? Then what if I later decided this to be an AJAX link? Okay, seems like this belongs to templates. But how do I do such a complex replacement with routing from a template? Should I write a custom template filter?</strong></p>
  
  <p>Just for the reference, a <code>Tweet</code> contains its <code>Tag</code>s in a field called <code>tags</code>.</p>