将主键传递给窗体temp

2024-10-04 11:24:44 发布

您现在位置:Python中文网/ 问答频道 /正文

这是这个问题的更新,如果我找到了这个问题的完整答案,我会修改它-How do I 'Autofill' a CreateView field

我有一个Artist模型,我现在正在尝试使用ArtistComment模式和modal div上的CreateView表单向DisplayView添加一个注释功能。我想我真的很快就可以实现这个功能了,但是我在将主键从我的artistdetail.html页面传递到我的artistcomment_form.html模板时遇到了一个小问题。如有任何帮助,或阅读文档页面的提示,我们将不胜感激。在

在网址.py公司名称:

url(r'^artist-(?P<pk>[0-9]+)/$', login_required(views.ArtistDetailView.as_view()), name='artistdetail'),
url(r'^artist-(?P<pk>[0-9]+)/artistcomment/add/$', login_required(views.ArtistCommentCreate.as_view()), name='artistcomment-add'),

在视图.py公司名称:

^{pr2}$

在artistdetail.html公司名称:

<p id="commentfooter"><a href="{% url 'events:artistcomment-add' artist.id %}">Add A New Comment</a></p>

艺术评论_发件人.html公司名称:

{% block body %}
<div class="col-md-12">
<div class="panel panel-default">
  <div class="panel-body">
    <form class="form-horizontal" action="{% url 'events:artistcomment-add' pk %}" method="post" enctype="multipart/form-data" onSubmit="CloseModal();">
      {% csrf_token %}
      {% include "events/form-template.html" %}
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <button type="submit" class="btn btn-success">Submit</button>
        </div>
      </div>
    </form>
  </div>
</div>

据我所知,在我的artistcomment_form.html中输入'pk'的地方就是我引用存储在URL中的主键的地方。我试过各种组合艺术家.id艺术家艺术家.pk但这些都是盲目的猜测,我一无所获。有人能帮我解释一下我遗漏了什么吗?在


Tags: divform名称addidurlartisthtml
1条回答
网友
1楼 · 发布于 2024-10-04 11:24:44

你需要做的就是:

class ArtistCommentCreate(CreateView):
# ...

def get_context_data(self, **kwargs):
        context = super(ArtistCommentCreate, self).get_context_data(**kwargs)
        context['pk'] = # Whatever you want to add here (e.g. self.object.artist_id)
        return context

最好将整个艺术家模型作为一个整体加载到上下文中,并添加一个模板(artistcomment.html)在你的CreateView中,因为它需要一个。我认为可以将带有iFrame的视图作为弹出窗口加载到页面中(我从未尝试过)。 另外,你不需要在你的评论表格中填写“行动”栏位。在

相关问题 更多 >