gpt4 book ai didi

javascript - Django 表单操作参数中 PK 的替代方案

转载 作者:行者123 更新时间:2023-12-02 23:48:10 25 4
gpt4 key购买 nike

我有两个具有父子关系的模型:IdeaComment。我使用 DRF 和嵌套 DataTables 将这些模型提供给浏览器。要创建评论,必须知道相应的想法 ID。创建新评论的按钮如下所示,其中 parentObjDataIdea 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>

单击时,会发生以下情况:

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)
}
})
}

这有效,并且当单击每个按钮时都会发送对正确 URL 的请求。这些 View 演示了成功请求后应该发生的情况:

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 解析为以下形式:

<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">&times;</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>

这是网址:

url(r'^platform/ideas/comments/(?P<parent_id>\d+)/create/$', views.idea_comment_create, name='idea_comment_create'),

parent_id 是问题所在。如果我将一个数字硬编码到 partial_idea_comment_create.html 中,一切都会很好(除了注释会指向错误的想法)。现在的情况是,我收到此错误:

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/$']

对于不依赖父实例的普通pk,我会这样做:

<form method="post" action="{% url 'idea_update' form.instance.pk %}">

我应该如何将 parent_id 动态加载到该 URL 中?

我认为这个问题部分存在:How do I pass a parent id as an fk to child object's ModelForm using generic class-based views in Django?但我没能让它为我工作。

最佳答案

问题是您没有在模板上下文中传递 parent_id。这是您应该在 save_comment_form_create 方法中执行的操作:

...
context = {'form': form, 'parent_id': parent_id}
data['html_form'] = render_to_string(template_name, context, request=request)
return JsonResponse(data)

关于javascript - Django 表单操作参数中 PK 的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55754709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com