title: Django Alternative to PK in Form Action Parameter
body: <p>I have two models in a parent-child relationship: <code>Idea</code> and <code>Comment</code>. I am using DRF and nested DataTables to serve these models to the browser. To create a comment, the corresponding idea ID must be known. The button to create a new comment looks like this with <code>parentObjData</code> being the <code>Idea</code> id:</p>
  
  <pre><code>&lt;button type="button" class="btn btn-primary js-create-idea-comment" data-url="/platform/ideas/comments/' + parentObjData + '/create/"&gt;&lt;span class="fa fa-plus"&gt;&lt;/span&gt; New Comment&lt;/button&gt;
  </code></pre>
  
  <p>When clicked, this happens: </p>
  
  <pre><code>var loadForm = function () {
    var btn = $(this)
    $.ajax({
      url: btn.attr("data-url"),
      type: 'get',
      dataType: 'json',
      beforeSend: function () {
        $("#modal-activity").modal()
      },
      success: function (data) {
        $("#modal-activity .modal-content").html(data.html_form)
      }
    })
  }
  </code></pre>
  
  <p>This works, and a request to the proper URL is sent when each button is clicked. What's supposed to happen with a successful request is demonstrated by these views:</p>
  
  <pre><code>def save_comment_form_create(request, form, template_name, parent_id):
      data = dict()
      if request.method == 'POST':
          if form.is_valid():
              instance = form.save(commit=False)
              instance.created_by_id = request.user.id
              instance.idea_id = parent_id
              form.save()
              data['form_is_valid'] = True
              comments = IdeaComment.objects.all()
              data['html_idea_comment_list'] = render_to_string('ic/includes/partial_idea_comment_list.html', {
                  'comments': comments
              })
          else:
              data['form_is_valid'] = False
      context = {'form': form}
      data['html_form'] = render_to_string(template_name, context, request=request)
      return JsonResponse(data)
  
  
  def idea_comment_create(request, parent_id):
      idea_id = parent_id
      if request.method == 'POST':
          form = IdeaCommentForm(request.POST)
      else:
          form = IdeaCommentForm()
      return save_comment_form_create(request, form, 'ic/includes/partial_idea_comment_create.html', idea_id)
  </code></pre>
  
  <p><code>partial_idea_comment_create.html</code> resolves to this form:</p>
  
  <pre><code>&lt;form method="post" action="{% url 'idea_comment_create' parent_id %}" class="js-idea-comment-create-form"&gt;
    {% csrf_token %}
    &lt;div class="modal-header"&gt;
      &lt;h4 class="modal-title"&gt;New Comment&lt;/h4&gt;
      &lt;button type="button" class="close" data-dismiss="modal" aria-label="Close"&gt;
        &lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;
      &lt;/button&gt;
    &lt;/div&gt;
    &lt;div class="modal-body"&gt;
      {% include 'ic/includes/partial_idea_comment_form.html' %}
    &lt;/div&gt;
    &lt;div class="modal-footer"&gt;
      &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt;
      &lt;button type="submit" class="btn btn-primary"&gt;Create&lt;/button&gt;
    &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And here is the URL: </p>
  
  <pre><code>url(r'^platform/ideas/comments/(?P&lt;parent_id&gt;\d+)/create/$', views.idea_comment_create, name='idea_comment_create'),
  </code></pre>
  
  <p><code>parent_id</code> is the issue. If I hard code a number into <code>partial_idea_comment_create.html</code>, everything works great (except comments go to the wrong idea). The way it is now, I get this error:</p>
  
  <pre><code>NoReverseMatch at /platform/ideas/comments/1/create/
  Reverse for 'idea_comment_create' with arguments '('',)' not found. 1 pattern(s) tried: ['platform/ideas/comments/(?P&lt;parent_id&gt;\\d+)/create/$']
  </code></pre>
  
  <p>For a normal <code>pk</code> that doesn't rely on a parent instance, I would do something like this:</p>
  
  <pre><code>&lt;form method="post" action="{% url 'idea_update' form.instance.pk %}"&gt;
  </code></pre>
  
  <p>How should I dynamically load the <code>parent_id</code> into that URL?</p>
  
  <p>I think this question gets partially there: <a href="https://stackoverflow.com/questions/24978121/how-do-i-pass-a-parent-id-as-an-fk-to-child-objects-modelform-using-generic-cla">How do I pass a parent id as an fk to child object&#39;s ModelForm using generic class-based views in Django?</a> but I haven't been able to get it to work for me.</p>