title: Template Tag not working with csrf_token
body: <p>I am attempting to create a custom template tag to display a comment.  When I try submitting a reply through the form attached to each comment, I get an error:</p>
  
  <p><em>Forbidden (403)
  CSRF verification failed. Request aborted.
  Reason given for failure:
      CSRF token missing or incorrect.</em></p>
  
  <p>The csrf token works on the form with the download and follow button, but not on the form generated from the template tag.  Any help would be appreciated.</p>
  
  <p>DocumentView</p>
  
  <pre><code>def DocumentView(request, doc_id):
      context = {}
      user = None
      if request.user.is_authenticated():
          email = request.user.email
          id = request.user.id
          user = get_user_information(email=email)
      else:
          user = None
  
      context['user'] = user
  
      if request.method == 'POST':
          user_id = request.POST.get('submit', '')
  
  
      doc = Doc.objects.get(id=doc_id)
      doc_version = DocVersion.objects.filter(doc__id=doc_id).latest('id')
      doc_title = doc.response_title
      allow_comments = doc.allow_comments
      comments = {}
      if user['user_type'] == 'DA':
          for comment in Comment.objects.filter(doc_id=doc_id):
              comments[comment.id] = {'content': comment.content, 'reply_to': comment.reply_to, 'user_id': comment.user.id}
      else:
          for comment in Comment.objects.filter(doc_id=doc_id, is_flagged=False):
              comments[comment.id] = {'content': comment.content, 'reply_to': comment.reply_to, 'user_id': comment.user.id}
      context['comments'] = comments
      context['allow_comments'] = allow_comments
      context['doc_title'] = doc_title
      print context
      return render(request, 'doc/document.html', context)
  </code></pre>
  
  <p>comment_tags.py</p>
  
  <pre><code>from django.template import Library, Node, Context, Variable
  from django.template.loader import get_template
  from django import template
  
  register = Library()
  
  @register.tag()
  def comments(parser, token):
      user_id, profile_pic, reply_id, comment_id, content, info = None, None, None, None, "", ""
  
      for index, x in enumerate(token.split_contents()):
          if x == 'user_id':
              user_id = token.split_contents()[index + 1]
          if x == 'profile_pic':
              profile_pic = token.split_contents()[index + 1]
          if x == 'content':
              content = token.split_contents()[index + 1]
          if x == 'info':
              info = token.split_contents()[index + 1]
          if x == 'reply_id':
              reply_id = token.split_contents()[index + 1]
          if x == 'comment_id':
              comment_id = token.split_contents()[index + 1]
  
      return CommentNode(user_id, profile_pic, comment_id, content, info, reply_id)
  
  
  
  
  class CommentNode(template.Node):
      def __init__(self, user_id, profile_pic, comment_id, content, info, reply_id):
          self.user_id = template.Variable(user_id)
          self.profile_pic = template.Variable(profile_pic)
          self.content = template.Variable(content)
          self.info = template.Variable(info)
          self.comment_id = template.Variable(comment_id)
  
          if reply_id != None:
              self.reply_id = template.Variable(reply_id)
          else:
              self.reply_id = reply_id
  
      def render(self, context):
          t = get_template("srd/comment.html")
  
          if self.reply_id == None:
              return t.render({
                      'user_id': self.user_id.resolve(context),
                      'profile_pic': self.profile_pic.resolve(context),
                      'comment_id': self.comment_id.resolve(context),
                      'content': self.content.resolve(context),
                      'info': self.info.resolve(context),
                      'reply_id': self.reply_id
                  }
              )
          else:
              return t.render({
                  'user_id': self.user_id.resolve(context),
                  'profile_pic': self.profile_pic.resolve(context),
                  'comment_id': self.comment_id.resolve(context),
                  'content': self.content.resolve(context),
                  'info': self.info.resolve(context),
                  'reply_id': self.reply_id.resolve(context)
              })
  </code></pre>
  
  <p>document.html</p>
  
  <pre><code>{% load static %}
  {% load comment_tags %}
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;{{ doc_title }}&lt;/title&gt;
      &lt;link rel="stylesheet" type="text/css" href="{% static "stylesheets/doc.css" %}"&gt;
      &lt;script src="{% static "javascript/jquery-3.2.1.min.js" %}" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script src="{% static "javascript/doc.js" %}" type="text/javascript"&gt;&lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div class="main_div"&gt;
          &lt;div class="content_div"&gt;
              &lt;div class="info_div"&gt;
                  &lt;h2&gt;[{{ generic_name }}]&lt;/h2&gt;
                  &lt;label&gt;{{ doc_title }} &lt;a href="edit"&gt;[Edit]&lt;/a&gt;&lt;/label&gt;
  
              &lt;/div&gt;
              &lt;div class="doc_div"&gt;
                  &lt;div class="doc_preview"&gt;
                  &lt;/div&gt;
                  &lt;div class="doc_preview_info"&gt;
                      &lt;form method="post"&gt;
                          {% csrf_token %}
                          &lt;input type="hidden" value="{{ user.id }}" /&gt;
                          &lt;input type="submit" value="Donwload PDF" /&gt;&lt;br/&gt;
                          &lt;input type="submit" value="Follow" /&gt;
                      &lt;/form&gt;
  
                  &lt;/div&gt;
              &lt;/div&gt;
              {% if allow_comments %}
              &lt;div class="comment_div" id="comment_div"&gt;
                  {% for commentid, comment in comments.items %}
                      {% if comment.reply_to %}
                          {% comments comment_id commentid user_id comment.user_id reply_id comment.reply_to.id profile_pic "hi" content comment.content info "XYZ" %}
                      {% else %}
                          {% comments comment_id commentid user_id comment.user_id profile_pic "hi" content comment.content info "XYZ" %}
                      {% endif %}
                  {% endfor %}
              &lt;/div&gt;
              {% endif %}
          &lt;/div&gt;
          &lt;div class="ad_div"&gt;
              &lt;div class="ad"&gt;&lt;/div&gt;
              &lt;div class="ad"&gt;&lt;/div&gt;
              &lt;div class="ad"&gt;&lt;/div&gt;
              &lt;div class="ad"&gt;&lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>comment.html</p>
  
  <pre><code>&lt;div class="comment"&gt;
      &lt;div class="profile_pic"&gt;
      {{ user_id }}
      &lt;/div&gt;
      &lt;div class="comment_content"&gt;
          &lt;div class="top_bar"&gt;
              &lt;div class="comment_info"&gt;
                  {{ info }}
              &lt;/div&gt;
              &lt;div class="flag"&gt;
                  &lt;a href="#"&gt;Flag&lt;/a&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="comment_text"&gt;
              {{ content }}
          &lt;/div&gt;
          &lt;div class="reply_bar"&gt;
              &lt;a id="reply_link_{{ comment_id }}" class="reply_link" href="#"&gt;Reply&lt;/a&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="reply" id="reply_{{ comment_id }}"&gt;
      &lt;form method="post"&gt;
          {% csrf_token %}
          &lt;input type="hidden" value="{{ reply_id }}"/&gt;
          &lt;input name="reply_text" type="text" /&gt;
          &lt;input type="submit" value="Post" /&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>