我正在尝试创建项目列表,每个项目都有一个按钮来单击并更新该项目。我正在尝试使用bootstrap模式进行更新。我可以让它工作,问题是当我点击更新按钮它不起作用,因为它没有将PK传递给我的URL,任何想法来解决这个问题?
这是我的观点
class ResearcherExperienceListView(LoginRequiredMixin, ListView):
model = Researcher
template_name = 'researcher/researcher_list.html'
def get_queryset(self):
queryset = super(ResearcherExperienceListView, self).get_queryset()
queryset = Researcher.objects.get(pk=self.kwargs['pk'])
print(self.kwargs)
return queryset
这是我的ListView模板
{% extends "base.html" %}
{% block content %}
<div class="container">
<div class="row" style="padding-top: 5%">
<div class="col-lg-10 col-lg-offset-1 col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2 ">
<h1 class="title text-center">Experiencia em Areas de conhecimento</h1>
<table class="table table-hover">
<thead>
<tr>
<th>Area de Conhecimento</th>
<th>Ações</th>
</tr>
</thead>
<tbody>
{% for item in object_list.experience.all %}
<tr>
<td>
{{item}}
</td>
<td>
<a data-toggle="modal" href="{% url 'researcher_experience_update' pk=item.pk %}" data-target="#{{item.pk}}" data-tooltip>
<i class="fa fa-edit fa-2x"></i>
</a>
</td>
</tr>
<div class="modal fade " id="{{item.pk}}" tabindex="-1" role="dialog">
{% include "researcher/experiencetime_form.html" %}
</div>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% endblock content %}
我的更新视图
class ResearcherExperienceUpdateView(LoginRequiredMixin, UpdateView):
model = ExperienceTime
form_class = ExpirienceTimeForm
这是我的更新模板。
{% load crispy_forms_tags %}
<div class="modal-dialog" role="document">
<div class="modal-content">
<h2 class="text-center card-title" style="padding-top:10px">Informações Gerais</h2>
<form method="post" action="update/" style="padding:20px">
{% csrf_token %}
{{ form|crispy }}
<input class="btn btn-primary btn-block" type="submit" value="Update" />
</form>
</div>
</div>
网址
re_path(r'^experience/(?P<pk>\d+)/$', ResearcherExperienceListView.as_view(), name="researcher_experience"),
re_path(r'^experience/update/(?P<pk>\d+)/$', ResearcherExperienceUpdateView.as_view(), name="researcher_experience_update"),
答案 0 :(得分:0)
感谢您的帮助
我确实使用get_context_data来解决问题,然后在我的表单操作中我做了类似这样的动作=“/ researcher / experience / update / {{pk}} /” 我不知道它是否正确但它现在有效