I have two models in a parent-child relationship: Idea
and Comment
. 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 parentObjData
being the Idea
id:
<button type="button" class="btn btn-primary js-create-idea-comment" data-url="/platform/ideas/comments/' + parentObjData + '/create/"><span class="fa fa-plus"></span> New Comment</button>
When clicked, this happens:
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)
}
})
}
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:
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)
partial_idea_comment_create.html
resolves to this form:
<form method="post" action="{% url 'idea_comment_create' parent_id %}" class="js-idea-comment-create-form">
{% csrf_token %}
<div class="modal-header">
<h4 class="modal-title">New Comment</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{% include 'ic/includes/partial_idea_comment_form.html' %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Create</button>
</div>
</form>
And here is the URL:
url(r'^platform/ideas/comments/(?P<parent_id>\d+)/create/$', views.idea_comment_create, name='idea_comment_create'),
parent_id
is the issue. If I hard code a number into partial_idea_comment_create.html
, everything works great (except comments go to the wrong idea). The way it is now, I get this error:
NoReverseMatch at /platform/ideas/comments/1/create/
Reverse for 'idea_comment_create' with arguments '('',)' not found. 1 pattern(s) tried: ['platform/ideas/comments/(?P<parent_id>\\d+)/create/$']
For a normal pk
that doesn't rely on a parent instance, I would do something like this:
<form method="post" action="{% url 'idea_update' form.instance.pk %}">
How should I dynamically load the parent_id
into that URL?
I think this question gets partially there: How do I pass a parent id as an fk to child object's ModelForm using generic class-based views in Django? but I haven't been able to get it to work for me.